Hi,
I'm trying to automate repititive filters using a MACRO but is unsuccessful. Appreciate if you could review my script below.
Thanks,
Blade
=======================
What are you trying to do exactly? What is the benefit of the recursive macro call here exactly?
Post example test data - in the form of a datastep, and what you want as output. That code doesn't make any sense.
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);
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);
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);
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;
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.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.