Help using Base SAS procedures

macros, inserting %let strings into code

Accepted Solution Solved
Reply
Contributor
Posts: 56
Accepted Solution

macros, inserting %let strings into code

I have code

%let idn = b;

%let Strike = 149;

%productData;

%MACRO productData;

DATA product_%idn;

set home.euro_c_%strike_1109;

   %idn_datetime = Options_Trade_date_Time;

   %idn_price = options_trade_price;

   keep %idn_datetime %idn_price;

RUN;

%MEND;

which does not work, how do I get the %idn and %strike into my code?

Also if I have sets of %idn and %strike like

b, 149

c, 148

d, 147

.

.

m, 138

I dont want to manually change the %let statements and then re-run then code, how can I run the macro over and over again, one time for each set of %idn and %strike ?


Accepted Solutions
Solution
‎05-01-2012 05:50 PM
Contributor
Posts: 73

Re: macros, inserting %let strings into code

Reread the code - Replaced '%' with '&' inside the data step.

%macro loopmacro(idn, strike);
  DATA product_&idn.;
  set home.euro_c_&strike._1109;
     &idn._datetime = Options_Trade_date_Time;
     &idn._price = options_trade_price;
     keep &idn._datetime &idn._price;
  RUN;
%MEND loopmacro;

%loopmacro(b, 149);
%loopmacro(c, 148);
%loopmacro(d, 147);
...
%loopmacro(m, 138);

View solution in original post


All Replies
Contributor
Posts: 73

Re: macros, inserting %let strings into code

If I understand your question correctly, you'll want to do something like the following:

%macro loopmacro(idn, Strike);
  %productData;
  %MACRO productData;
  DATA product_%idn;
  set home.euro_c_%strike_1109;
     %idn_datetime = Options_Trade_date_Time;
     %idn_price = options_trade_price;
     keep %idn_datetime %idn_price;
  RUN;
%MEND;

%loopmacro(b, 149);
%loopmacro(c, 148);
%loopmacro(d, 147);
...
%loopmacro(m, 138);

Contributor
Posts: 56

Re: macros, inserting %let strings into code

I dont understand, you have %macro twice in there but only %mend once?

how do i call this macro?

Contributor
Posts: 56

Re: macros, inserting %let strings into code

if i have code

%macro loopmacro(idn, Strike);

  %productData;

  %MACRO productData;

  DATA product_%idn;

  set home.euro_c_%strike_1109;

     %idn_datetime = Options_Trade_date_Time;

     %idn_price = options_trade_price;

     keep %idn_datetime %idn_price;

  RUN;

  %MEND;

%MEND;

%loopmacro(b, 149);

%loopmacro(c, 148);

and highlight it all and hit run, I get no data sets? how do i run this thing?

Contributor
Posts: 73

Re: macros, inserting %let strings into code

Try this:

%macro loopmacro(idn, Strike);
  DATA product_%idn;
     set home.euro_c_%strike_1109;
     %idn_datetime = Options_Trade_date_Time;
     %idn_price = options_trade_price;
     keep %idn_datetime %idn_price;
  RUN;
%MEND;

%loopmacro(b, 149);
%loopmacro(c, 148);
%loopmacro(d, 147);
...
%loopmacro(m, 138);

Contributor
Posts: 56

Re: macros, inserting %let strings into code

if I run that, nothing happens, my log editor just says

119  %loopmacro(b, 149);

120  %macro loopmacro(idn, Strike);

121    DATA product_%idn;

122       set home.euro_c_%strike_1109;

123       %idn_datetime = Options_Trade_date_Time;

124       %idn_price = options_trade_price;

125       keep %idn_datetime %idn_price;

126    RUN;

127  %MEND;

128  %loopmacro(b, 149);

129  %loopmacro(c, 148);

130  %loopmacro(d, 147);

131  %loopmacro(m, 138);

but i get no data sets appearing in my work folder

Contributor
Posts: 73

Re: macros, inserting %let strings into code

You define a maco variable with a '%' but you call a macro variable with a '&'.

%macro loopmacro(idn, strike);
  DATA product_&idn.;
  set home.euro_c_&strike._1109;
     &idn._datetime = Options_Trade_date_Time;
     &idn._price = options_trade_price;
     keep &idn._datetime &idn._price;
  RUN;
%MEND loopmacro;

%loopmacro(b, 149);
%loopmacro(c, 148);
%loopmacro(d, 147);
...
%loopmacro(m, 138);

Contributor
Posts: 56

Re: macros, inserting %let strings into code

I still dont understand; ive tried

&loopmacro(b,149);

or

&loopmacro(idn, strike);

or

&loopmacro;

but nothing happens, how do I call this macro?

Contributor
Posts: 56

Re: macros, inserting %let strings into code

%macro loopmacro(idn, Strike);

  DATA product_%idn;

     set home.euro_c_%strike_1109;

     %idn_datetime = Options_Trade_date_Time;

     %idn_price = options_trade_price;

     keep %idn_datetime %idn_price;

  RUN;

%MEND loopmacro;

%loopmacro(b, 149);

%loopmacro(c, 148);

%loopmacro(d, 147);

%loopmacro(m, 138);

so would

&loopmacro(b, 149);

call this macro, as I cant seem to call this thing

Solution
‎05-01-2012 05:50 PM
Contributor
Posts: 73

Re: macros, inserting %let strings into code

Reread the code - Replaced '%' with '&' inside the data step.

%macro loopmacro(idn, strike);
  DATA product_&idn.;
  set home.euro_c_&strike._1109;
     &idn._datetime = Options_Trade_date_Time;
     &idn._price = options_trade_price;
     keep &idn._datetime &idn._price;
  RUN;
%MEND loopmacro;

%loopmacro(b, 149);
%loopmacro(c, 148);
%loopmacro(d, 147);
...
%loopmacro(m, 138);

Contributor
Posts: 56

Re: macros, inserting %let strings into code

still dont understand, with the code

%macro loopmacro(idn, strike);

  DATA product_&idn.;

  set home.euro_c_&strike._1109;

     &idn._datetime = Options_Trade_date_Time;

     &idn._price = options_trade_price;

     keep &idn._datetime &idn._price;

  RUN;

%MEND loopmacro;

%loopmacro(b, 149);

%loopmacro(c, 148);

%loopmacro(d, 147);

I highlight a run. Now how do I call this macro? with

&loopmacro(b,149); or &loopmacro or &loopmacro(idn,strike);;

does not work

Super User
Posts: 10,843

Re: macros, inserting %let strings into code

The macro call would be the %loopmacro(b,149);

Try setting macro options before the call to see if you're getting resolution errors in the macro:

options symbolgen mprint;

%loopmacro(b, 149);

options nosymbolgen nomprint;

The option will put text in the LOG of the resolved macro variables and the resulting syntax.

& is for referencing macro VARIABLES, % is for calling macros and macro functions.

Contributor
Posts: 56

Re: macros, inserting %let strings into code

i tried that but what is meant to happen, i ran the code

%macro loopmacro(idn, strike);

  DATA product_&idn.;

  set home.euro_c_&strike._1109;

     &idn._datetime = Options_Trade_date_Time;

     &idn._price = options_trade_price;

     keep &idn._datetime &idn._price;

  RUN;

%MEND loopmacro;

and then ran

options symbolgen mprint;

%loopmacro(b, 149);

options nosymbolgen nomprint;

and the log just reads

210  %macro loopmacro(idn, strike);

211    DATA product_&idn.;

212    set home.euro_c_&strike._1109;

213       &idn._datetime = Options_Trade_date_Time;

214       &idn._price = options_trade_price;

215       keep &idn._datetime &idn._price;

216    RUN;

217  %MEND loopmacro;

218  options symbolgen mprint;

219  %loopmacro(b, 149);

220  options nosymbolgen nomprint;

but thats all, what do I infer from this?

Respected Advisor
Posts: 3,147

Re: macros, inserting %let strings into code

After you restart your SAS session as Tom has suggested,  Try the following code:

/*This is where you store parameters supposedly*/

data list;

  input a$ b$;

  cards;

  b 149

  c 148

  d 147

  m 142

  ;

/*To get the total number of pairs of your parameter*/

  data _null_;

    call symput('count',nobs);

    set list nobs=nobs;

    stop;

    run;

/*This is your core macro*/

%macro loopmacro(idn=, strike=);

  DATA product_&idn.;

  set home.euro_c_&strike._1109;

     &idn._datetime = Options_Trade_date_Time;

     &idn._price = options_trade_price;

     keep &idn._datetime &idn._price;

  RUN;

%MEND loopmacro;

/*This is to get your dynamic input of parameters in macro variable*/

%macro par;

     data _null_;

           set list;

          call symputx('a'||left(_n_), a);

          call symputx('b'||left(_n_),b);

      run;

%mend;

%par 

/*Now this is to get what you want hopefully*/

%macro final;

%do i=1 %to &count;

%loopmacro(idn=&&a&i, strike=&&b&i)

%end;

%mend;

%final

Regards,

Haikuo

Super User
Super User
Posts: 6,687

Re: macros, inserting %let strings into code

Close SAS and start over with one of the later examples where the typos and missing %MEND statements have been fixed.  You probably have the SAS compiler so confused by now it doesn't know whats what.  So it is probably just eating every line you give it looking for what ever end symbol it needs to complete the current statement.

Here is a little guidance:

You need to DEFINE the macro BEFORE you call it.  The %MACRO/%MEND pair define the macro.

To EXECUTE or CALL a macro you type it with % in front of its name.  %LOOPMACRO(a,b)  for example.

To EVALUATE a macro VARIABLE you reference it with an & in front of its name. &IDN for example.

(Note some people call macro variables "macros".  It is best to call the variables macro variables and avoid confusion with actual macros.)

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 16 replies
  • 862 views
  • 6 likes
  • 5 in conversation