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"
Where STATA is storing the loading factors ( Factor analysis using GSEM)Hi guys, I used "gsem" commend and "predict, latent" to estimate factor scores and the factors scor…
putpdf image errorAfter a logit, I am doing Code: lroc graph export "$charts\roc_y_1_x_`k'.png", replace putpdf imag…
Empty cells in some imputations of multiply imputed dataI have a data set that some colleagues and I need to do a survival analysis on. The outcome is time …
Do I need to add main effect in a regression model with a Difference value between two variables?Hi, Here is my regression model: regress y x1 x2 x3_x4; /* x3_x4 = x3 - x4 */ Do I need to add x3…
Problem with propensity score matchingHello - I am running the following code for propensity score matching and then running a model on th…
Subscribe to:
Post Comments (Atom)
0 Response to Generating New Variable with several values of one existing variable using "Wildcards"
Post a Comment