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"
Help/advice on importing large number of text files into StataDear all, I have a collection of around 2,400 PDFs of parliamentary debate transcriptions that I wo…
Elasticity estimates in Tobit regressionDear Statalisters This is my first post so please excuse if the question is not posed correctly. W…
Calculating effect sizes after MI and mixedHello All, I hope this message finds you well! After running the following code is there a way to …
Tabout Error - Conformability r(3200) Hello I am using running a series of crosstab tables using the following tabout command: Code: t…
year dummies in xtreg and pooled OLSHello, I ran both a fixed effects model and pooled OLS model on the same dataset. I am now wonderin…
Subscribe to:
Post Comments (Atom)
0 Response to Generating New Variable with several values of one existing variable using "Wildcards"
Post a Comment