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

I made macro %dataload

 

and want to load data once by using loop.. not that listing

 

%dataload(year=16,month=09);
%dataload(year=16,month=10);
%dataload(year=16,month=11);
%dataload(year=17,month=12);
%dataload(year=17,month=01);
%dataload(year=17,month=02);
%dataload(year=17,month=03);
%dataload(year=17,month=04);
%dataload(year=17,month=05);
%dataload(year=17,month=06);
%dataload(year=17,month=07);
%dataload(year=17,month=08);
%dataload(year=17,month=09);
%dataload(year=17,month=10);
%dataload(year=17,month=11);

 

 

can I use do loop?

for example

 

%let start_year=16

%let end_year=18

 

do i = start_year to end_year;

do j= 1 to 12;

%dataload(year=i,month=j)

end;

end;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You can. 

 

Is something not working for you?

 

CALL EXECUTE is usually simpler however.

 


@asdfsa wrote:

I made macro %dataload

 

and want to load data once by using loop.. not that listing

 

%dataload(year=16,month=09);
%dataload(year=16,month=10);
%dataload(year=16,month=11);
%dataload(year=17,month=12);
%dataload(year=17,month=01);
%dataload(year=17,month=02);
%dataload(year=17,month=03);
%dataload(year=17,month=04);
%dataload(year=17,month=05);
%dataload(year=17,month=06);
%dataload(year=17,month=07);
%dataload(year=17,month=08);
%dataload(year=17,month=09);
%dataload(year=17,month=10);
%dataload(year=17,month=11);

 

 

can I use do loop?

for example

 

%let start_year=16

%let end_year=18

 

do i = start_year to end_year;

do j= 1 to 12;

%dataload(year=i,month=j)

end;

end;


 

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

The macro PREprocessor does its work (generating code!) before any Base SAS code starts to run. Since your macro will leave Base SAS code in its place, that will destroy any data step logic.

To call a macro from a data step (using values created in the data step), use call execute().

gamotte
Rhodochrosite | Level 12

Hello,

 

You have to use macro loops (and use & to resolve macrovariables values) :

 

%let start_year=16
%let end_year=18

%do i = &start_year. %to &end_year.;
    %do j= 1 to 12;
        %dataload(year=&i.,month=&j.)
    %end;
%end;

 

Edit : Note that a macro loop can only be used inside a macro :

 

%macro mymacro;

 

/* macro code */

...

 

%mend;

 

%mymacro;

Reeza
Super User

You can. 

 

Is something not working for you?

 

CALL EXECUTE is usually simpler however.

 


@asdfsa wrote:

I made macro %dataload

 

and want to load data once by using loop.. not that listing

 

%dataload(year=16,month=09);
%dataload(year=16,month=10);
%dataload(year=16,month=11);
%dataload(year=17,month=12);
%dataload(year=17,month=01);
%dataload(year=17,month=02);
%dataload(year=17,month=03);
%dataload(year=17,month=04);
%dataload(year=17,month=05);
%dataload(year=17,month=06);
%dataload(year=17,month=07);
%dataload(year=17,month=08);
%dataload(year=17,month=09);
%dataload(year=17,month=10);
%dataload(year=17,month=11);

 

 

can I use do loop?

for example

 

%let start_year=16

%let end_year=18

 

do i = start_year to end_year;

do j= 1 to 12;

%dataload(year=i,month=j)

end;

end;


 

s_lassen
Meteorite | Level 14

As @gamotte already implied, you would have to use a macro loop. One possibility is to rewrite your %dataload macro to a recursive version:

%macro dataload(year=,month=,from=,to=);
  %if %length(&from) and %length(&to) %then %do;
    %do from=&from %to &to;
      %if %substr(&from,3)=13 %then /* new year */
        %let from=%eval(&from+88);
      %dataload(year=%substr(&from,1,2),month=%substr(&from,3));
      %end;
    %return;
    %end;
  /* here goes the contents of the original macro */
%mend;

You could then call the macro as

%dataload(from=1609,to=1711);

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 687 views
  • 2 likes
  • 5 in conversation