BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BrahmanandaRao
Lapis Lazuli | Level 10
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
1 ACCEPTED SOLUTION
8 REPLIES 8
Kurt_Bremser
Super User

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.

BrahmanandaRao
Lapis Lazuli | Level 10
data month;
do i= '01Jan2010'd to '31Dec2010'd;
month=put(month((i) ,monname3.));
output;
end;
run;
Kurt_Bremser
Super User

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.

BrahmanandaRao
Lapis Lazuli | Level 10
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  

BrahmanandaRao
Lapis Lazuli | Level 10

 

 

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

Anandkvn_0-1648281725561.png

 

BrahmanandaRao
Lapis Lazuli | Level 10
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;


Anandkvn_0-1648282654015.png

 

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
  • 8 replies
  • 1444 views
  • 0 likes
  • 2 in conversation