DATA Step, Macro, Functions and more

listing macro variable's values with prefix in a procedure

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

listing macro variable's values with prefix in a procedure

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!!


Accepted Solutions
Solution
‎02-22-2017 03:46 AM
Super User
Posts: 9,691

Re: listing macro variable's values with prefix in a procedure

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

%put &score ;

View solution in original post


All Replies
Super Contributor
Posts: 408

Re: listing macro variable's values with prefix in a procedure

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.

Solution
‎02-22-2017 03:46 AM
Super User
Posts: 9,691

Re: listing macro variable's values with prefix in a procedure

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

%put &score ;
Super Contributor
Posts: 408

Re: listing macro variable's values with prefix in a procedure

Nice and consice. I like it!
New Contributor
Posts: 4

Re: listing macro variable's values with prefix in a procedure

[ Edited ]

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

Super User
Posts: 9,691

Re: listing macro variable's values with prefix in a procedure


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

%put &score ;

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 144 views
  • 4 likes
  • 3 in conversation