SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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