Skip to content

Commit

Permalink
version 2.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jh-min committed Nov 10, 2019
0 parents commit 697e8cf
Show file tree
Hide file tree
Showing 4 changed files with 405 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# previous versions
history/

# for other purposes
*.PNG
*.txt
*.zip

# Eclipse Metadata
.metadata/

# Mac OS X clutter
*.DS_Store

# Windows clutter
Thumbs.db
281 changes: 281 additions & 0 deletions nscale.ado
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
*** version 2.1.2 5August2019
*** contact information: [email protected]

program nscale
version 10
syntax varlist [, GENerate(namelist) PREfix(name) Missing(numlist max=1) UP DOWN Reverse Tab noPOSTfix]

quietly {

if "`postfix'" == "nopostfix" {

if "`generate'" != "" | "`prefix'" != "" {

noisily Error 198 "option generate or prefix may not be combined with nopostfix"

}
else {
* gen(namelist) and pre(name): off
if "`missing'" != "" {

foreach var in `varlist' {
if "`up'" != "" & "`down'" != "" {
noisily Error 198 "option up may not be combined with down"
}
else if "`up'" != "" & "`down'" == "" {
summarize `var' if `var' < `missing'
replace `var' = (`var' - r(min))/(r(max) - r(min)) if `var' < `missing'
replace `var' = . if `var' >= `missing'
}
else if "`up'" == "" & "`down'" != "" {
summarize `var' if `var' > `missing'
replace `var' = (`var' - r(min))/(r(max) - r(min)) if `var' > `missing'
replace `var' = . if `var' <= `missing'
}
else {
summarize `var' if `var' != `missing'
replace `var' = (`var' - r(min))/(r(max) - r(min)) if `var' != `missing'
replace `var' = . if `var' == `missing'
}
if "`reverse'" != "" {
replace `var' = 1 - `var'
}
}

}
else {
* m(#): off
if "`up'" != "" | "`down'" != "" {
noisily Error 198 "option up or down requires option missing to be set"
}
else {
foreach var in `varlist' {
summarize `var'
replace `var' = (`var' - r(min))/(r(max) - r(min))
if "`reverse'" != "" {
replace `var' = 1 - `var'
}
}
}

}

foreach var in `varlist' {
label values `var' .
}

if "`tab'" != "" {
noisily tab1 `varlist'
}

}

}
else {

if "`generate'" != "" & "`prefix'" != "" {

noisily Error 198 "option generate may not be combined with prefix"

}
else if "`generate'" != "" & "`prefix'" == "" {

local n_vars : word count `varlist'
if `n_vars' < `: word count `generate'' {
noisily Error 103 "option generate(): too many names specified"
}
else if `n_vars' > `: word count `generate'' {
noisily Error 103 "option generate(): too few names specified"
}
forval i = 1/`n_vars' {
local ovar : word `i' of `varlist'
local nvar : word `i' of `generate'
clonevar `nvar' = `ovar'
label values `nvar' .
}

if "`missing'" != "" {

foreach var in `generate' {
if "`up'" != "" & "`down'" != "" {
noisily Error 198 "option up may not be combined with down"
}
else if "`up'" != "" & "`down'" == "" {
summarize `var' if `var' < `missing'
replace `var' = (`var' - r(min))/(r(max) - r(min)) if `var' < `missing'
replace `var' = . if `var' >= `missing'
}
else if "`up'" == "" & "`down'" != "" {
summarize `var' if `var' > `missing'
replace `var' = (`var' - r(min))/(r(max) - r(min)) if `var' > `missing'
replace `var' = . if `var' <= `missing'
}
else {
summarize `var' if `var' != `missing'
replace `var' = (`var' - r(min))/(r(max) - r(min)) if `var' != `missing'
replace `var' = . if `var' == `missing'
}
if "`reverse'" != "" {
replace `var' = 1 - `var'
}
}

}
else {
* m(#): off
if "`up'" != "" | "`down'" != "" {
noisily Error 198 "option up or down requires option missing to be set"
}
else {
foreach var in `generate' {
summarize `var'
replace `var' = (`var' - r(min))/(r(max) - r(min))
if "`reverse'" != "" {
replace `var' = 1 - `var'
}
}
}

}

if "`tab'" != "" {
noisily tab1 `generate'
}

}
else if "`generate'" == "" & "`prefix'" != "" {

local n_vars : word count `varlist'
forval i = 1/`n_vars' {
local ovar : word `i' of `varlist'
clonevar `prefix'`ovar' = `ovar'
label values `prefix'`ovar' .
}

if "`missing'" != "" {

foreach var in `varlist' {
if "`up'" != "" & "`down'" != "" {
noisily Error 198 "option up may not be combined with down"
}
else if "`up'" != "" & "`down'" == "" {
summarize `var' if `var' < `missing'
replace `prefix'`var' = (`var' - r(min))/(r(max) - r(min)) if `var' < `missing'
replace `prefix'`var' = . if `var' >= `missing'
}
else if "`up'" == "" & "`down'" != "" {
summarize `var' if `var' > `missing'
replace `prefix'`var' = (`var' - r(min))/(r(max) - r(min)) if `var' > `missing'
replace `prefix'`var' = . if `var' <= `missing'
}
else {
summarize `var' if `var' != `missing'
replace `prefix'`var' = (`var' - r(min))/(r(max) - r(min)) if `var' != `missing'
replace `prefix'`var' = . if `var' == `missing'
}
if "`reverse'" != "" {
replace `prefix'`var' = 1 - `prefix'`var'
}
}

}
else {
* m(#): off
if "`up'" != "" | "`down'" != "" {
noisily Error 198 "option up or down requires option missing to be set"
}
else {
foreach var in `varlist' {
summarize `var'
replace `prefix'`var' = (`var' - r(min))/(r(max) - r(min))
if "`reverse'" != "" {
replace `prefix'`var' = 1 - `prefix'`var'
}
}
}

}

if "`tab'" != "" {
local n_vars : word count `varlist'
local s_var : word 1 of `varlist'
local e_var : word `n_vars' of `varlist'
noisily tab1 `prefix'`s_var'-`prefix'`e_var'
}

}
else {
* no, gen(namelist) and pre(name): off
if "`missing'" != "" {

foreach var in `varlist' {
clonevar `var'_01 = `var'
if "`up'" != "" & "`down'" != "" {
noisily Error 198 "option up may not be combined with down"
drop `var'_01
}
else if "`up'" != "" & "`down'" == "" {
summarize `var' if `var' < `missing'
replace `var'_01 = (`var' - r(min))/(r(max) - r(min)) if `var' < `missing'
replace `var'_01 = . if `var' >= `missing'
}
else if "`up'" == "" & "`down'" != "" {
summarize `var' if `var' > `missing'
replace `var'_01 = (`var' - r(min))/(r(max) - r(min)) if `var' > `missing'
replace `var'_01 = . if `var' <= `missing'
}
else {
summarize `var' if `var' != `missing'
replace `var'_01 = (`var' - r(min))/(r(max) - r(min)) if `var' != `missing'
replace `var'_01 = . if `var' == `missing'
}
if "`reverse'" != "" {
replace `var'_01 = 1 - `var'_01
}
}

}
else {
* m(#): off
if "`up'" != "" | "`down'" != "" {
noisily Error 198 "option up or down requires option missing to be set"
}
else {
foreach var in `varlist' {
summarize `var'
clonevar `var'_01 = `var'
replace `var'_01 = (`var' - r(min))/(r(max) - r(min))
if "`reverse'" != "" {
replace `var'_01 = 1 - `var'_01
}
}
}

}

foreach var in `varlist' {
label values `var'_01 .
}

if "`tab'" != "" {
local n_vars : word count `varlist'
local s_var : word 1 of `varlist'
local e_var : word `n_vars' of `varlist'
noisily tab1 `s_var'_01-`e_var'_01
}

}

}

}

end

program define Error
version 10
args nr txt

dis as err `"{p}`txt'{p_end}"'
exit `nr'
end
29 changes: 29 additions & 0 deletions nscale.pkg
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
d 'NSCALE': module to scale data
d
d nscale provides a convenient way to recode variables to lie
d between 0 and 1 via dividing var-min(var) by max(var)-min(var).
d nscale can recode many variables at once and has options gen()
d and pre() which work in the similar manner as Stata’s default
d command recode does. It is also possible to store transformed
d values in existing variables with option nopost. nscale can set
d specific value or interval to missing (.) as well. For example,
d if a survey question q1 coded DK as 98 and miscellaneous as 99
d and you want to exclude those values from analysis, just type:
d nscale q1 , m(98) up. Then nscale sets all observations whose
d value is greater than or equal to 98 to missing (.). The option
d down combined with option m(#) works similarly. Moreover, nscale
d does reverse coding with option r and tabulates new variables
d with option t.
d
d KW: data management
d KW: scaling
d
d Requires: Stata version 10
d
d Distribution-Date: 20190805
d
d Author: JeongHoon Min , Sogang University
d Support: email plus1@@sogang.ac.kr
d
f nscale.ado
f nscale.sthlp
Loading

0 comments on commit 697e8cf

Please sign in to comment.