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
Replacing values when duplicatesDear all, I struggled to find a proper title for the following issue. I have the following panel da…
Graphical legendDear All, I have this graph: Array obtained by (Thanks to Nick Cox), Code: set scheme s1color bys…
Help on understanding Cross-sectional dependenceHello together, I need some help in understanding cross-sectional dependence regarding the use of D…
Return percentiles using simulate commandHi, I am trying to improvise an example from help of Code: simulate command as shown below Code:…
DID with multiple periods and treatmentsDear all, I am analysing the impacts of dung beetles (treatment) on livestock productivity (outcome…
Subscribe to:
Post Comments (Atom)
0 Response to Replacing comma separated values within a string variable with letters
Post a Comment