From 697e8cf47a7c89b2a0d54f83f768650b6d8ab42a Mon Sep 17 00:00:00 2001 From: jh-min Date: Mon, 11 Nov 2019 01:07:40 +0900 Subject: [PATCH] version 2.1.2 --- .gitignore | 16 +++ nscale.ado | 281 +++++++++++++++++++++++++++++++++++++++++++++++++++ nscale.pkg | 29 ++++++ nscale.sthlp | 79 +++++++++++++++ 4 files changed, 405 insertions(+) create mode 100644 .gitignore create mode 100644 nscale.ado create mode 100644 nscale.pkg create mode 100644 nscale.sthlp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..613a1de --- /dev/null +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/nscale.ado b/nscale.ado new file mode 100644 index 0000000..9adb4cd --- /dev/null +++ b/nscale.ado @@ -0,0 +1,281 @@ +*** version 2.1.2 5August2019 +*** contact information: plus1@sogang.ac.kr + +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 diff --git a/nscale.pkg b/nscale.pkg new file mode 100644 index 0000000..07de7ef --- /dev/null +++ b/nscale.pkg @@ -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 diff --git a/nscale.sthlp b/nscale.sthlp new file mode 100644 index 0000000..551a858 --- /dev/null +++ b/nscale.sthlp @@ -0,0 +1,79 @@ +{smcl} +{* *! version 2.1.2 5August2019}{...} +{viewerjumpto "Syntax" "nscale##syntax"}{...} +{viewerjumpto "Description" "nscale##description"}{...} +{viewerjumpto "Examples" "nscale##examples"}{...} + +{title:Title} + +{phang}{bf:nscale} {hline 2} Program to recode variables to lie between 0 and 1 + + +{marker syntax}{...} +{title:Syntax} + +{p 8 17 2} + {cmd:nscale} {varlist} + [{cmd:,} {it:options}] + + +{synoptset 20 tabbed}{...} +{synopthdr} +{synoptline} +{syntab:Main} +{synopt:{opt gen:erate(namelist)}}create {it:namelist} so that transformed values of the {it:n}th variable of {it:varlist} are stored in the {it:n}th variable of the {it:namelist}{p_end} +{synopt:{opt pre:fix(name)}}create new variables with prefix {it:name}{p_end} +{synopt:{opt nopost:fix}}replace existing variables{p_end} +{synopt:{opt m:issing(#)}}set observations to missing (.) if their value is equal to {it:#}; can be combined with {opt up} or {opt down}{p_end} +{synopt:{opt up}}set observations to missing (.) if their value is greater than or equal to the number assigned by {opth missing(#)}{p_end} +{synopt:{opt down}}set observations to missing (.) if their value is less than or equal to the number assigned by {opth missing(#)}{p_end} +{synopt:{opt r:everse}}reverse variable coding{p_end} +{synopt:{opt t:ab}}tabulate new variables{p_end} +{synoptline} +{p2colreset}{...} + +{p 4 6 2} +If neither {opth generate(varlist)}, {opth prefix(name)} nor {opt nopostfix} is set, {cmd:nscale} creates new variables with postfix {it:_01}.{p_end} +{p 4 6 2} +{opt nopostfix} may not be combined with {opth generate(varlist)} or {opth prefix(name)}.{p_end} +{p 4 6 2} +{opth generate(varlist)} may not be combined with {opth prefix(name)}.{p_end} +{p 4 6 2} +{opt up} may not be combined with {opt down}.{p_end} + + +{marker description}{...} +{title:Description} + +{pstd} + {cmd:nscale} provides a convenient way to {cmd:recode} variables in {varlist} + to lie between 0 and 1 via dividing {it:var}-{opt min(var)} by {opt max(var)}-{opt min(var)}. + {cmd:nscale} can {cmd:recode} many variables at once and has options {opth generate(namelist)} and {opth prefix(name)} + which work in the similar manner as Stata’s default command {cmd:recode} does. + It is also possible to store transformed values in existing variables with option {opt nopostfix}. + +{pstd} + {cmd:nscale} can set specific value or interval to missing (.) as well. For example, + if a survey question q1 coded {it:DK} as {it:98} and {it:miscellaneous} as {it:99} + and you want to exclude those values from analysis, just type: {cmd:nscale q1} , {opt m(98)} {opt up}. + Then {cmd:nscale} sets all observations whose value is greater than or equal to {it:98} to missing (.). + The option {opt down} combined with option {opth missing(#)} works similarly. + +{pstd} + Moreover, {cmd:nscale} does reverse coding with option {opt reverse} and tabulates new variables with option {opt tab}. + + +{marker examples}{...} +{title:Examples} + +{phang}{cmd:. nscale var}{p_end} + +{phang}{cmd:. nscale var1 var2 , gen(nv1 nv2)}{p_end} + +{phang}{cmd:. nscale {varlist} , pre(new_)}{p_end} + +{phang}{cmd:. nscale {varlist} , m(9)}{p_end} + +{phang}{cmd:. nscale {varlist} , r t}{p_end} + +{phang}{cmd:. nscale {varlist} , nopost}{p_end}