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
xtabond2 question on large N small TI am in the process of running two different xtabond2 models, and have a question regarding the spec…
New version of xcontract (YET AGAIN) on SSCThanks yet again to Kit Baum, a new version of the xcontract package (superseding last year's versio…
Loop Panel Regression to save one coefficient and visualizeDear all, I am new to Stata and could use some help. I have a Panel-Data set where I try to analyze …
Tracking changes in dataset with conditionDear reader, I tracked executives across firms and retained those individual executives who were em…
How draw a number from a given vector of distribution?Hello everyone, and happy new year! I searched everywhere for an answer but could not find any, so …
Subscribe to:
Post Comments (Atom)
0 Response to Replacing comma separated values within a string variable with letters
Post a Comment