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?
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.