DATA Step, Macro, Functions and more

substr on character and numeric variables to create new variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

substr on character and numeric variables to create new variable

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

 


Accepted Solutions
Solution
2 weeks ago
Super User
Posts: 6,928

Re: substr on character and numeric variables to create new variable

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


All Replies
Solution
2 weeks ago
Super User
Posts: 6,928

Re: substr on character and numeric variables to create new variable

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

Super User
Posts: 2,505

Re: substr on character and numeric variables to create new variable

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

 

 

 

Occasional Contributor
Posts: 13

Re: substr on character and numeric variables to create new variable

Hi ChrisNZ ,

 

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

Super User
Posts: 2,505

Re: substr on character and numeric variables to create new variable

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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