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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 814 views
  • 1 like
  • 2 in conversation