DATA Step, Macro, Functions and more

Get each last day of preceding months

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Get each last day of preceding months

Hello,

 

I wanted to create a dataset called timestamps that lists the last day of each month of the preceding 6 months. My solution for this is:

DATA timestamps;
	d = today();
	format d date9.;
  	DO i=0 TO 5;
  		d = intnx('month',d,-i)-1;
  		output;
  	END;
run;

 

However, I get this as result:
sas.jpg
 
If I "rerun the loop manually", I get the day I expect. E.g. this code
DATA timestamps;
	d = today();
	format d date9.;
  	d = intnx('month',d,-1)-1;
run;
gives: 31MAY2017
 
Why do I get 30APR2017 for i =1 in the loop? What is wrong in my loop?
Thanks for the help!

Kind regards,
philip

Accepted Solutions
Solution
‎07-06-2017 04:43 AM
Super User
Super User
Posts: 7,942

Re: Get each last day of preceding months

At a guess your changing the variable your working on, try:

data timestamps;
  d = today();
  format d dt date9.;
  do i=0 to 5;
    dt=intnx('month',d,-i)-1;
    output;
  end;
run;

View solution in original post


All Replies
Solution
‎07-06-2017 04:43 AM
Super User
Super User
Posts: 7,942

Re: Get each last day of preceding months

At a guess your changing the variable your working on, try:

data timestamps;
  d = today();
  format d dt date9.;
  do i=0 to 5;
    dt=intnx('month',d,-i)-1;
    output;
  end;
run;
☑ This topic is solved.

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

Discussion stats
  • 1 reply
  • 96 views
  • 1 like
  • 2 in conversation