Hi Everyone;
I am trying to create a new variable based on the values of another variable using substr function. My original dataset looks like this:
data try;
input ID status /* add $ to see second output*/ ;
datalines;
1 178
2 178
3 .
4 .
5 B55
6 B55
8 22253
9 22253
10 N65
11 N66
12 22256
;
run;
I am using the following function to create the new variable. This function gives the expected results only when I input the status variable in character format ( $ ). My original dataset has status as numerical variable and I have tried too many formats and its not working. The output I get status as numeric and as character are shown below
data want;
set try;
status1 = 'No';
if substr(status,1,3) in ("178","b55")
OR substr(status,1,2) in ("N6")
OR substr(status,1,4) in ("2225")
then status1='Yes';
run;
proc print data= want ;run;
ObsID status status11234567891011
1 | 178 | No |
2 | 178 | No |
3 | . | No |
4 | . | No |
5 | . | No |
6 | . | No |
8 | 22253 | No |
9 | 22253 | No |
10 | . | No |
11 | . | No |
12 | 22256 | No |
second output
Obs ID status status11234567891011
1 | 178 | Ye |
2 | 178 | Ye |
3 | No | |
4 | No | |
5 | B55 | No |
6 | B55 | No |
8 | 22253 | Ye |
9 | 22253 | Ye |
10 | N65 | Ye |
11 | N66 | Ye |
12 | 22256 | Ye |
example of formats I tried
data new;
set try;
status_c =put(status, 8.);
run;
any ideas
Perhaps not a complete solution, but a few ideas that you need to implement.
First, you can see you don't get all the characters in "Yes". You need to add this before assigning STATUS1 a value:
length status1 $ 3;
Second, to left-hand justify the digits, you are close but have to add another function:
status_c = left(put(status, 8.));
You still have the basic problem to solve about having both numeric and character values. You need to use a character variable in that case.
Finally, you can simplify the comparisons by adding a colon. For example,
if status in : ("178", "b55", "N6", "2225") then status1="Yes";
Perhaps not a complete solution, but a few ideas that you need to implement.
First, you can see you don't get all the characters in "Yes". You need to add this before assigning STATUS1 a value:
length status1 $ 3;
Second, to left-hand justify the digits, you are close but have to add another function:
status_c = left(put(status, 8.));
You still have the basic problem to solve about having both numeric and character values. You need to use a character variable in that case.
Finally, you can simplify the comparisons by adding a colon. For example,
if status in : ("178", "b55", "N6", "2225") then status1="Yes";
1. My original dataset has status as numerical variable
I am confused. How do you get a value of B55 if your variable is numeric?
2. This
status_c = put(status, 8.);
should probably be left justified:
status_c = put(status, 8. -l);
Hi ChrisNZ ,
I am importing my dataset from R. My last resort was to copy and paste the data into a data step .
Have you seen this?
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.