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

I have a macro variable:

%let par = var1 var2 var3;

Now I want to use those values in a procedure like this

proc score data=test type=parms score=par out=score; var W_var1 W_var2 W_var3; run;

For example, if var1=age then I want to list W_age in the var statement. I tried var W_∥ but it doesn't work. Can anyone help me with a simple solution here? Thanks a bunch!!

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
%let par = var1 var2 var3;
%let score=%sysfunc(prxchange(s/(\w+)/W_$1/,-1,&par));

%put &score ;

View solution in original post

5 REPLIES 5
jklaverstijn
Rhodochrosite | Level 12

Hi,

 

The code W_&par would merely give you "W_var1 var var". You must parse out the macro variable par and prefix the W_ for each word in the value. Based upon a sample code in the SAS usage notes, this woujld be a good start:

 

options mprint;
%macro test(string);
  %let word_cnt=%sysfunc(countw(%superq(string)));   /* Count # words */
proc score data=test type=parms score=par out=score;
var
  %do i = 1 %to &word_cnt;
    %let var&i=%qscan(%superq(string),&i,%str( ));   /* get i'th word  */
    W_&&var&i   /* add prefix */
  %end;
; run;
%mend test;

%let par = var1 var2 var3;
%test (&par);

Hope this helps,

 

Jan.

Ksharp
Super User
%let par = var1 var2 var3;
%let score=%sysfunc(prxchange(s/(\w+)/W_$1/,-1,&par));

%put &score ;
jklaverstijn
Rhodochrosite | Level 12
Nice and consice. I like it!
ShinCrayons
Fluorite | Level 6

what if I want to add '1' at the end of every values of &par? I'm confusing about how prxchange function work.

Ksharp
Super User

%let par = var1 var2 var3;
%let score=%sysfunc(prxchange(s/(\w+)/W_\11/,-1,&par));

%put &score ;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 3182 views
  • 4 likes
  • 3 in conversation