BookmarkSubscribeRSS Feed
ieva
Pyrite | Level 9
I have a data similar to these:

data data;
input value $ name $15.;
datalines;
4.1% abc 4.1% a
3.2% a2 b c 3.2%
1.5% ab.cd 1.5% a ab
;
run;

What I need to do is to make a new variable from variable „name” keeping only beginning of it till the numbers with followed with % sign (that are as separate variable „value” here). So I would like to have values: abc, a2 b c, ab.cd. As the number of characters are different and also name includes special characters and spaces, I think it isn’t possible to use simple substr or scan functions.

So what I tried to do is to create macro variable that countains these values:
proc sql noprint;
select value into: val_list separated by ' ' from data;
quit;
%put &val_list;

After that I use this macro variable, scanning values one by one and want to use scan function to ask SAS to make a new variable „changed_name” keeping only values before those symbols that are in value read from macro variable &var_leave.

%macro calc;
data test;
set data;
format macronum $8.;

%let num=1;
%let var_leave=%scan(&val_list,&num , %str( ));
%do %while (&var_leave ne);
if &num=_n_ then do;

macronum="&var_leave";
changed_name=scan(name,1, "&var_leave");

output;
end;

%let num=%eval(&num+1);
%let var_leave=%scan(&val_list,&num);
%end;
run;
%mend;
%calc;

Of course, that doesn‘t work 🙂 As I understand defining macro variable works as expected, but scan function later doesn‘t do what I wanted.

Maybe you could advice me where is the error in my codes or suggest some better way to do this task?
3 REPLIES 3
polingjw
Quartz | Level 8
Here is one way to solve the problem just by using substr, find, and anyspace functions:
[pre]
data data;
input value $ name $15.;
datalines;
4.1% abc 4.1% a
3.2% a2 b c 3.2%
1.5% ab.cd 1.5% a ab
;
run;

data data1;
set data;
name1=substr(name, 1, anyspace(substr(name, 1, find(name, '%')-1), -1*(find(name, '%')-1)));
run;

proc print data=data1;
run;

[/pre]
polingjw
Quartz | Level 8
Actually, now that I think about it, it would be even better to use the anyalpha function instead of the anyspace function, just in case there is no space between the character and the number. Message was edited by: polingjw
ieva
Pyrite | Level 9
Thanks, it works perfectly! 🙂

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3 replies
  • 644 views
  • 0 likes
  • 2 in conversation