Hello,
I must modify the following %Sysfunc-TranWrd-Code to work in a %Macro-step. Dataset "Cmd" works, dataset "Cmd2" is not working.
Data A;
Input Var_List_TD $ @@;
Datalines;
AFD GDC GEX
;
Run;
Proc SQL NoPrint;
Select Var_List_TD Into :Var_List_TD Separated By ' ' From A;
Quit;
%Put **&Var_List_TD.**;
Data Cmd; * <-- works;
Length Cmd $200.;
Cmd=Cat(" OutArray ","x%Sysfunc(TranWrd(&Var_List_TD.,%Str( ),%Str(", "x)))",";"); Output;
Run;
%Macro Macro_Cmd; * <-- does not work;
Data Cmd2;
Length Cmd $200.;
Cmd=Cat(" OutArray ","x%Sysfunc(TranWrd(&Var_List_TD.,%Str( ),%Str(", "x)))",";"); Output;
Run;
%Mend;
%Macro_Cmd;
%Let dummy="x%Sysfunc(TranWrd(&Var_List_TD.,%Str( ),%Str(", "x)))";
%Put *&dummy.**;
%Macro Macro_Cmd; * <-- this what I am trying to avoid;
Data Cmd3;
Length Cmd $200.;
Cmd=Cat(" OutArray ",&dummy.,";"); Output;
Run;
%Mend;
%Macro_Cmd;
Thanks&kind regards
OK, if it must be a macro ...
%macro must (varlist=);
data want;
Cmd="Outarray &varlist";
Output;
run;
%mend must;
%must (varlist=AFD GDC GEX)
Sorry, not even going to try on that. What is it your trying to do, I can pretty much guarentee that the moment code looks like % && % xyz there is a far better way of doing things.
I think I can make my question a bit shorter. The goal is to get the "AFD GDC GEX" into a row in a dataset beginning with "Outarray " and ending with ";" - within a %macro-step.
This is, I start out with:
Data Have;
Input Var_List_TD $ @@;
Datalines;
AFD GDC GEX
;
Run;
****
The goal is:
Data Want;
Cmd="Outarray AFD GDC GEX"; Output; * <-- but not this way, but dynamic & within a %macro statement;
Run;
(?)
Something like this, if your example is more complicated, then post accurate example data.
Data Have;
Input Var_List_TD $ @@;
Datalines;
AFD GDC GEX
;
Run;
data tmp;/*_null_;*/
set have end=last;
retain sp;
length sp $2000;
if _n_=1 then sp=strip(var_list_td);
else sp=cat(strip(sp)," ",strip(var_list_td));
if last then call symput('LIST',cats('Out array ',strip(sp));
run;
I have to agree. This looks like an exercise in complicating a simple task, and adding macro language to preserve job security. Start here:
Data A;
infile cards truncover;
Input Var_List_TD $200.;
Datalines;
AFD GDC GEX
;
Then add a few lines after the INPUT statement to construct the string you want. No macro language needed, but you do need to understand what the output is supposed to look like. A DATA step can apply character functions much more easily than macro language.
I am afraid adding up the rows won't suffice.
But my first question was way too complicated.
What I would like to achieve is, to get this within a %Macro and %Mend:
%Let Var_List_TD=AFD GDC GEX;
Data Cmd;
Length Cmd $200.;
Cmd=Cat(" OutArray ","%Sysfunc(TranWrd(&Var_List_TD.,%Str( ),%Str( ", ")))",";"); Output;
Run;
OK, if it must be a macro ...
%macro must (varlist=);
data want;
Cmd="Outarray &varlist";
Output;
run;
%mend must;
%must (varlist=AFD GDC GEX)
Yes, back in the day V6 and before, functionality was a bit more limited, therefore macro was used a lot to get round things. Nowadays though, if find I look at 95% of macro code and see it is not worth it, simple datasteps and straightforward coding can do most of it.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.