DATA Step, Macro, Functions and more

%SysFunc & TranWrd in a %Macro-step

Accepted Solution Solved
Reply
Super Contributor
Posts: 336
Accepted Solution

%SysFunc & TranWrd in a %Macro-step

[ Edited ]

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

 


Accepted Solutions
Solution
‎10-29-2015 02:06 AM
Super User
Posts: 5,081

Re: %SysFunc & TranWrd in a %Macro-step

OK, if it must be a macro ...

 

 

%macro must (varlist=);

   data want;

      Cmd="Outarray &varlist";

      Output;

   run;

%mend must;

%must (varlist=AFD GDC GEX)

View solution in original post


All Replies
Super User
Super User
Posts: 7,401

Re: %SysFunc & TranWrd in a %Macro-step

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.

Super Contributor
Posts: 336

Re: %SysFunc & TranWrd in a %Macro-step

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;

 

(?)

Super User
Super User
Posts: 7,401

Re: %SysFunc & TranWrd in a %Macro-step

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;
Super User
Posts: 5,081

Re: %SysFunc & TranWrd in a %Macro-step

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.

Super Contributor
Posts: 336

Re: %SysFunc & TranWrd in a %Macro-step

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;

 

 

Solution
‎10-29-2015 02:06 AM
Super User
Posts: 5,081

Re: %SysFunc & TranWrd in a %Macro-step

OK, if it must be a macro ...

 

 

%macro must (varlist=);

   data want;

      Cmd="Outarray &varlist";

      Output;

   run;

%mend must;

%must (varlist=AFD GDC GEX)

Super Contributor
Posts: 336

Re: %SysFunc & TranWrd in a %Macro-step

I recognize I have got to simplify this. I did not see this at first. Sorry.
Super User
Super User
Posts: 7,401

Re: %SysFunc & TranWrd in a %Macro-step

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.  

☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 725 views
  • 3 likes
  • 3 in conversation