Hello Statalist,
I am trying to generate a new variable based on an existing variable. The existing variable (for economic sectors) has 1,300 individual observations, and consists of a number from 01 to 99000. To make sectors more manageable I want to create a new variable which uses only the first two numbers of the sector. So if the old variable is 01 or 011 or 01110 or 011120 or 0113 or 01113, the new one should be "01". The same for all values in my old variable starting with "02" etc.
What I would need is a sort of wildcard, to do for instance:
gen sector_new = 1 if sector_old == 01*
replace sector_new = 2 if sector_old == 02*
etc
However as I understand it wildcards can only be used for variables and not for singular values, so the above command does not work and I am looking for a way to implement this.
The problem why I can't use the inrange command or inlist command (or simply tell state to gen a new var b if a (my old var) is between 0 and for instance 01900) is that the value of my old variable can be 2,3,4 or 5 digits. So if I would say "generate new variable if the value is between 0 and 01900", it would also include for instance 02, 021, 022 etc, which I do not want as these need to go in a separate category.
I hope the explanation is understandable, and would very much appreciate your help.
Kind regards
Elisabeth
Related Posts with Generating New Variable with several values of one existing variable using "Wildcards"
doubt in svyset for a 3-stage samplingHello everyone, I'm analyzing some country-level data that was sampled in the following way: 1) Co…
Negative margins value with penalized maximumlikelihood estimatesHello, I've tried the logit with a binary variable but I found out that the sample size is only 95. …
Export Stata Results to Word using TaboutDear Statalisters, I wish to export the results of the tabulate command to a Word file using the ta…
Reshape wideHi all, I would like to reshape wide my unbalanced database which looks as follow: Code: * Exam…
Variance function regression using HAPC (age-period-cohort) Hi Statalist, I am attempting to run a model on stata, specifically, a Variance function regressio…
Subscribe to:
Post Comments (Atom)
0 Response to Generating New Variable with several values of one existing variable using "Wildcards"
Post a Comment