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

Is there a way to reduce by one day on each loop\macro interaction?

I tried INTNX('day',TODAY(), -i,'s') but it seems to not accept  "i" as one of the parameters instead of a number.

 

%macro test(X);

%do i = 1 %to 30;

PROC SQL;
   CREATE TABLE WORK.A AS
   SELECT t1.ID,
          t1.DATE_A
      FROM WORK.B t1
      WHERE t1.DATE_A <= INTNX('day',TODAY(),-i,'s');
QUIT;

/*CONTINUE HERE...*/

%end;

%mend test;

%test

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

No. That would be &i not i.

 

Macro variables have a & in front of them, SQL or data step variables do not. So i is a data step or sql variable not your macro loop variable. 

 

UCLA introductory tutorial on macro variables and macros
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/


View solution in original post

4 REPLIES 4
Reeza
Super User
WHERE t1.DATE_A <= INTNX('day',TODAY(),-&i,'s');

I think you just need to use the macro version of i, so &i.

I don't actually know where i is declared elsewhere in your code so no idea where its getting that value from.
hy2000
Calcite | Level 5

Is from here 

%do i = 1 %to 30;

 

Reeza
Super User

No. That would be &i not i.

 

Macro variables have a & in front of them, SQL or data step variables do not. So i is a data step or sql variable not your macro loop variable. 

 

UCLA introductory tutorial on macro variables and macros
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/


hy2000
Calcite | Level 5

Thanks, Reeza this article helped me a lot, after reading it I actually achieve what I wanted, here’s the final code:

 

options nodate nonumber nocenter formdlim="-";
data a;
  input  date var_a;
format date ddmmyy10.;         
datalines;
22101 1
22101 1
22101 1
22101 1
22101 1
22100 1
22100 1
22100 1
22100 1
22100 1
22100 1
22099 1
22099 1
22099 1
22099 1
22099 1
22099 1
22099 1
;
run;
proc sql;
select date,
var_a
from work.a;
quit;
data b;
format date ddmmyy10.;
var_a = .;
run;
proc sql;
delete from b;
quit;
%let now = today();
%put &now;
%macro insert;
%do i = 1 %to 5;
	proc sql;
	insert into b
	select * from a
	where date < intnx('day', intnx('day', today(), -5), &i);
	quit;
%end;
%mend;
%insert

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1220 views
  • 1 like
  • 2 in conversation