BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
skipper
Calcite | Level 5

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 ?

1 ACCEPTED SOLUTION

Accepted Solutions
robby_beum
Quartz | Level 8

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

16 REPLIES 16
robby_beum
Quartz | Level 8

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

skipper
Calcite | Level 5

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

how do i call this macro?

skipper
Calcite | Level 5

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?

robby_beum
Quartz | Level 8

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

skipper
Calcite | Level 5

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

robby_beum
Quartz | Level 8

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

skipper
Calcite | Level 5

I still dont understand; ive tried

&loopmacro(b,149);

or

&loopmacro(idn, strike);

or

&loopmacro;

but nothing happens, how do I call this macro?

skipper
Calcite | Level 5

%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

robby_beum
Quartz | Level 8

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

skipper
Calcite | Level 5

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

ballardw
Super User

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.

skipper
Calcite | Level 5

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?

Haikuo
Onyx | Level 15

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

Tom
Super User Tom
Super User

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

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 16 replies
  • 1903 views
  • 6 likes
  • 5 in conversation