BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Jordani
Obsidian | Level 7

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

1178No
2178No
3.No
4.No
5.No
6.No
822253No
922253No
10.No
11.No
1222256No

 

second output

 

Obs ID status status11234567891011

1178Ye
2178Ye
3 No
4 No
5B55No
6B55No
822253Ye
922253Ye
10N65Ye
11N66Ye
1222256Ye

example of formats I tried

 

 

 

data new;
set try;
status_c =put(status, 8.);
run;

any ideas

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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";

View solution in original post

4 REPLIES 4
Astounding
PROC Star

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";

ChrisNZ
Tourmaline | Level 20

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);

 

 

 

Jordani
Obsidian | Level 7

Hi ChrisNZ ,

 

I am importing my dataset from R. My last resort was to copy and paste the data into a data step .

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 7636 views
  • 0 likes
  • 3 in conversation