Write and run SAS programs in your web browser

MACRO Error in SAS Studio

Reply
New Contributor
Posts: 2

MACRO Error in SAS Studio

Hi,

 

I'm trying to automate repititive filters using a MACRO but is unsuccessful. Appreciate if you could review my script below.

 

Thanks,

Blade

 

=======================

 

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
55
56 %macro a(STOCK);
57 %a(PSE);
58
59 data _&stock; set PSE; where _NAME_="&stock"; run;
60
61 %mend a;
62
63 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
75
Super Contributor
Posts: 490

Re: MACRO Error in SAS Studio

What are you trying to do exactly? What is the benefit of the recursive macro call here exactly?

Esteemed Advisor
Esteemed Advisor
Posts: 6,685

Re: MACRO Error in SAS Studio

Post example test data - in the form of a datastep, and what you want as output.  That code doesn't make any sense.

Grand Advisor
Posts: 16,303

Re: MACRO Error in SAS Studio

You have your macro invocation inside your macro, that doesn't make sense. Macros are precompiled code, similar to a function in many other languages, but not a function.

 

*Declare Macro;
%macro a(STOCK);


 data _&stock; 
set PSE; 
where _NAME_="&stock"; 
run;

%mend a;

*make macro run;
%a(PSE);
Esteemed Advisor
Esteemed Advisor
Posts: 6,685

Re: MACRO Error in SAS Studio

Hi Reeza,

 

Actually it is possible to call a macro within itself.  They are not generally compiled - unless you specifically compile to a library - and can be thought of like text replacements, they are recursive and should be used with caution as they can create infinitie loops.  In the usual fibonacci example these are used, this isn't fibonacci just shows it can be done:

%macro Tmp (a);
  %if &a>0 %then %tmp (%eval(&a.-1));
  %put &a.;
%mend Tmp;

%tmp (1);
New Contributor
Posts: 2

Re: MACRO Error in SAS Studio

Thanks for the responses. To clarify, I have a mother dataset named PSE and would like to extract/filter records from it using a field labelled as _NAME_. The macro script is also expected to produce output files (_XXXX).

 

I've updated the script below to put comments. It seem to run successfully in SAS Base/EG, but not here in Studio. Thanks again for your time and patience.

 

======================================

 

%macro a(STOCK);


data _&stock; /*NEW DATASET CREATED*/
  set PSE;  /*MOTHER DATASET*/
  where _NAME_="&stock"; /*FILTER APPLIED*/
run;

 

%mend a;


%a(PSE);
%a(2GO);
%a(EEI);

Esteemed Advisor
Esteemed Advisor
Posts: 6,685

Re: MACRO Error in SAS Studio

Post your log.  What doesn't work?  Remember the two software are different, do you have the relvan datasets available in each software?  Also note, that your code would look better if you did:

data pse 2go eei fallout;
  set pse;
  select (_name_);
    when ("PSE") output pse;
    when ("2GO") output 2go;
    when ("EEI") output eei;
    otherwise output fallout;
  end;
run;

  

I would avoid using _NAME_ as a variable, this is a SAS automated variable.  

Of course you can automate it if there is a changing amount of datasets:

data have;
  _name_="EEI"; output;
  _name_="2GO"; output;
  _name_="ABC"; output;
run;

proc sql noprint; 
  select  distinct "_"||strip(_NAME_)
  into    :DLIST separated by " "
  from    HAVE;
  create table LOOP as
  select  distinct cat("when ('",strip(_NAME_),"') output _",strip(_NAME_),";") as CMD
  from    HAVE;
quit;

data _null_;
  set loop end=last;
  if _n_=1 then call execute("data &DLIST. fallout; set have; select(_name_); ");
  call execute(cmd);
  if last then call execute(' otherwise output fallout; end; run;');
run;
Grand Advisor
Posts: 16,303

Re: MACRO Error in SAS Studio

Please explain what you mean by it doesn't run in SAS Studio.
Post a Question
Discussion Stats
  • 7 replies
  • 276 views
  • 1 like
  • 4 in conversation