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
estimating dynamic panel data models when the data set features a moderate N and moderate T.What are the methods of estimation that can be used when the data set features a moderate N and mode…
Why do I get different results for fe and gmm2s estimation using (xt)ivreg2?Dear Statalisters, I am trying to analyze the relationship between corruption and the distribution …
Descriptive statistics; graph illustrating effects on Y before and after treatment (Stata 16)Dear Statalisters, I have an unbalanced panel dataset with approx. 12,300 observations and am analy…
Help replacing individual data from household data, using macros over variable names.Hi there, I have only recently started using Stata to edit data, instead of using the default datas…
Dtalink: Coditioning probability matchingHi, I am currently using the Stata dtalink-package in order to match data from two sources. As I cr…
Subscribe to:
Post Comments (Atom)
0 Response to Replacing comma separated values within a string variable with letters
Post a Comment