Hello,
I have a question regarding string variables.
I have a string variable (calendar) that represents a monthly calendar over a three year period (36 months) for people in my survey. Each month is currently separated by commas. Each number or letter represent a monthly status.
For example:
,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5 ,5,5,0,0,0,0,0,0,0,0
,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9 ,9,9,9,9,9,9,9,9,9,9
Eventually I want to have a 36-character string variable so that I can manipulate the data and find out what everyone’s status was at the same month. The variable I want for the two observations above would end up looking like:
55555555555555555555555555500000000
99999999999999999999999999999999999
This is easy enough- I know how to remove commas and trim leading spaces etc.
My problem is that some of the monthly statuses are coded as numbers above 10, and I need to replace any monthly value above 10 with a single character (probably a letter since numbers 0-9 are already taken) so that every observation’s string variable is the same length. I need to standardize the length of the string like this so that later I can extract the 12th month value across observations and compare them, reshape, etc.
So, in the following two observations for the calendar variable, I need the code to replace the 14s and 30s with another value, such as “X” and “Y”, respectively:
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,14,14,14,14,14,14,14
P,P,P,P,P,P,30,30,30,30,30,30,14,14,14,14,14,14,14 ,14,14,14,14,14,14,14,14,14,14,14,14,B,P,P,P,P
I need the code to make the above two values become:
00000000000000000000000000000XXXXXXX
PPPPPPYYYYYYXXXXXXXXXXXXXXXXXXXBPPPP
Any help would be greatly appreciated.
Dana
Related Posts with Replacing comma separated values within a string variable with letters
Can anyone help me loop these lengthy codes? Code: matrix table1=J(6,6,.) ttest preaddition == postaddition matrix table1[1,1] = r(mu_1) matrix…
In Stata, how to transfer matrix to LaTeX table?I have a matrix, which contains all the numbers I want. Now I need to design a table in LaTeX and us…
can anyone share a template of beginning of do file?Currently, I just have three lines at the beginning of my do file. What do you think? Code: clear …
when should I use program?I start to read program tutorial in Stata. But I'm still confused when I should use program. …
Three-level multilevel model-Stata is running a random slope for something constantI'm currently working with panel data with five waves of data. Persons are nested within teams, so I…
Subscribe to:
Post Comments (Atom)
0 Response to Replacing comma separated values within a string variable with letters
Post a Comment