BookmarkSubscribeRSS Feed
Blade
Calcite | Level 5

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
7 REPLIES 7
mohamed_zaki
Barite | Level 11

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

Reeza
Super User

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);
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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);
Blade
Calcite | Level 5

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);

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
Reeza
Super User
Please explain what you mean by it doesn't run in SAS Studio.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

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
  • 7 replies
  • 1512 views
  • 1 like
  • 4 in conversation