12-22-2015 07:59 AM
I'm trying to automate repititive filters using a MACRO but is unsuccessful. Appreciate if you could review my script below.
12-22-2015 10:16 AM
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);
12-23-2015 04:59 AM
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);
12-22-2015 11:15 PM
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.
data _&stock; /*NEW DATASET CREATED*/
set PSE; /*MOTHER DATASET*/
where _NAME_="&stock"; /*FILTER APPLIED*/
12-23-2015 05:09 AM
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;