04-14-2015 01:04 PM
Can anyone tell me the code how to format/update observations.
I have the following dataset:
John MK 3
Ciara MK 4
Jack LR 9
Alam MK 5
Mike LR 8
basically I want to create a new variable and group the onservation as MK (where it start with MK%) and LR (where it start with LR%) which should look like:
Name Group Group2
John MK 3 MK
Ciara MK 4 MK
Jack LR 9 LR
Alam MK 5 MK
Mike LR 8 LR
How do I do it with format and how do I do it without formating?
04-14-2015 01:13 PM
Format works with a list or range of values. Since character values do not behave as most people want when attempting to use ranges you generally have to list every value you expect to encounter. Something like
value $ group
"MK 1","MK 2","MK 3", "MK 4" = "MK"
"LR 8", "LR 9" = "LR"
Then to create a new variable in a datastep:
Group2 = put(group,$group.);
One major advantage of formats is that you may not need to create an additional format.
You are likely to be better off with string manipulation functions such as SUBSTR. Again in a data step:
Group2 = upcase(substr(group,1,2));
04-14-2015 01:20 PM
If all of your values begin with MK or LR, you could try:
length group2 $ 2;
group2 = group;
Since GROUP2 is defined as having a shorter length, it only has room to store the first two characters of GROUP.