DATA Step, Macro, Functions and more

loop

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

loop

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;


Accepted Solutions
Solution
yesterday
Super User
Posts: 23,695

Re: loop

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


All Replies
Super User
Posts: 10,217

Re: loop

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

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super Contributor
Posts: 340

Re: loop

[ Edited ]

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;

Solution
yesterday
Super User
Posts: 23,695

Re: loop

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;


 

PROC Star
Posts: 263

Re: loop

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);
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 79 views
  • 2 likes
  • 5 in conversation