DATA Step, Macro, Functions and more

Loop between two macro variables

Reply
Occasional Contributor
Posts: 19

Loop between two macro variables

 

 

I created below macro to generate few datasets based on date macro.

 

 

%macro pull(date);

proc sql;

create table new&date as 

select * from acct

where date=&date.;

quit;



%mend;

So if i want to create dataset for 20170101 20170201 20170301 20170401 20170501, all i can do is use below macro

 

 

%macro pull(20170101)

%macro pull(20170201)

%macro pull(20170301)

%macro pull(20170401)

%macro pull(20170501)

What i am planning now is create two macro variables

 

%let begin=20170101;

%let end =20170501;

 

and create datasets based on begin and end using loop. Is it possible to do that.

PROC Star
Posts: 1,218

Re: Loop between two macro variables

Posted in reply to suresh123

1. Why do you want to split up your data like this? 

 

2. You don't need a macro to do this. Do a google search for CALL EXECUTE Examples. That will get you in the right direction.

Occasional Contributor
Posts: 19

Re: Loop between two macro variables

Splitting to see each month reports
PROC Star
Posts: 2,319

Re: Loop between two macro variables

Posted in reply to suresh123

This should help:

%macro loop(startdt,enddt);
 %local date;
 %do date=&startdt. %to &enddt.; 
  %if %substr(&date.,5,2)=13 %then %let date=%eval(&date-12+100); 
  proc sql;
   create table new&date. as 
   select * from acct
   where date=&date.;
  quit;
 %end;
%mend;         
%loop(201701,201703);

Using intnx() is the proper way, but this is easier to understand and works just as well.

 

Occasional Contributor
Posts: 19

Re: Loop between two macro variables

I dont want to use %macro or call execute....
PROC Star
Posts: 1,218

Re: Loop between two macro variables

Posted in reply to suresh123

You post a macro yourself in your question?

Occasional Contributor
Posts: 19

Re: Loop between two macro variables

%let beg="01jan2000"d;
%let end="01jan2001"d;
%macro Test;
%do date=&beg. %to &end.;
proc sql;
create table IPw_&date. as
select *
from sample
where date=&date. quit;
%end;
%mend;
%Test;


Super User
Super User
Posts: 7,942

Re: Loop between two macro variables

[ Edited ]
Posted in reply to suresh123

@suresh123 wrote:
I dont want to use %macro or call execute....

If is difficult to generate dataset names without using some type of code generation. The third choice for code generation is to use a data step to write the code to a text file and then %include the file.

filename code temp;
data _null_;
  set have ;
  by date ;
  where date between &start and &end ;
  file code ;
  if first.date then put .......... ;
run;
%include code;

A fourth way to dynamically generate dataset names is to use HASH objects.  But hash objects are limited to amount of data that can be stored in memory.

 

WHY do you need separate dataset?  What value is being added by splitting the data into many separate files?

Occasional Contributor
Posts: 19

Re: Loop between two macro variables

Can you elaborate this %include code in my scenario
Occasional Contributor
Posts: 19

Re: Loop between two macro variables

So what iam looking is, I dont want to enter the dates manually through %pull(20170101,20170501). Its need to pick from %let macro
Super User
Super User
Posts: 7,942

Re: Loop between two macro variables

Posted in reply to suresh123

@suresh123 wrote:
So what iam looking is, I dont want to enter the dates manually through %pull(20170101,20170501). Its need to pick from %let macro

Are you just talking about doing something like this?

%let begin=20170101;
%let end =20170501;
%pull(&begin,&end)
PROC Star
Posts: 2,319

Re: Loop between two macro variables

Posted in reply to suresh123

And I agree  with @draycut btw.

PROC Star
Posts: 1,218

Re: Loop between two macro variables

Posted in reply to suresh123

A non macro way

 

proc sql;
   create table dates as
   select distinct date
   from sashelp.stocks
   where "01jan2000"d <= date <= "01jan2001"d;
quit;

data callstack;
   set dates;
   code=cats("data Stocks_", put(date, date9.), "; set sashelp.stocks; where date= ", date, ";run;");
   call execute(code);
run;
Occasional Contributor
Posts: 19

Re: Loop between two macro variables

Is there any chance we can do it using do loop to create multiple datatsets between the dates
PROC Star
Posts: 1,218

Re: Loop between two macro variables

Posted in reply to suresh123

What do you want those data sets to contain?

Ask a Question
Discussion stats
  • 17 replies
  • 269 views
  • 2 likes
  • 5 in conversation