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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

user24feb
Barite | Level 11

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;

 

(?)

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
Astounding
PROC Star

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.

user24feb
Barite | Level 11

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;

 

 

Astounding
PROC Star

OK, if it must be a macro ...

 

 

%macro must (varlist=);

   data want;

      Cmd="Outarray &varlist";

      Output;

   run;

%mend must;

%must (varlist=AFD GDC GEX)

user24feb
Barite | Level 11
I recognize I have got to simplify this. I did not see this at first. Sorry.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.  

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 8 replies
  • 9448 views
  • 3 likes
  • 3 in conversation