data month;
st='01Jan2010';
enddt='31Dec2010';
do i=st to enddt;
month=intnx('Month',st,i,'same');
output;
end;
run;
Required output
Date Month 01-Jan-10 Jan 01-Feb-10 Feb 01-Mar-10 Mar 01-Apr-10 Apr 01-May-10 May 01-Jun-10 Jun 01-Jul-10 Jul 01-Aug-10 Aug 01-Sep-10 Sep 01-Oct-10 Oct 01-Nov-10 Nov 01-Dec-10 Dec
That's just the order of statements. Fix that.
You cannot use character values as start and end of an iterative DO loop.
To make your strings into date literals, add a trailing d:
st = '01Jan2010'd;
Use the MONNAME format to get the character month from a SAS date value.
data month;
do i= '01Jan2010'd to '31Dec2010'd;
month=put(month((i) ,monname3.));
output;
end;
run;
As I mentioned, the MONNAME format is for date values, not month numbers. Please read the documentation.
And if you want to loop over months, not dates, you will need a different loop. Either DO WHILE and the INTNX function to increment, or a loop from 1 to 12, building dates with the MDY function.
data date_range;
do Date ='01Jan2010'd to '31Dec2010'd ;
format Date : date11.;
if Date =intnx('month',Date,0,'b') then output;
end;
run;
data month;
set date_range;
month=put(Date,monname3.);
run;
I tried my idea is it hard coded
Use a DO WHILE:
date = '01jan2022'd;
do while (date le '01dec2022'd);
output;
date = intnx('month',date,1,'b');
end;
data dw;
date = '01jan2022'd;
do while (date le '01dec2022'd);
output;format date month: date11.;
Month=put(date,monname3.);
date = intnx('month',date,1,'b');
end;
run;
As per your code i got output
That's just the order of statements. Fix that.
data dw;
date = '01jan2022'd;
do while (date le '01dec2022'd);
Month=put(date,monname3.);
output;
format date month: date11.;
date = intnx('month',date,1,'b');
end;proc print noobs;
run;
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!
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.
Ready to level-up your skills? Choose your own adventure.