BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NewUsrStat
Pyrite | Level 9

Hi guys, 

I have to create a dataset containing months from 01JAN2014 to 31DEC2022 like this: 

 

01JAN2014

01FEB2014

01MAR2014

.......................

01JAN2022

.......................

01DEC2022

 

 Can anyone help me please? 

 

Thank you in advance

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
antonbcristina
SAS Employee

There's a few ways to achieve this. You could use DO loops to go through all of the years and months, and the MDY function to construct the date using the first of each month like so:

 

data want;
	do y=2014 to 2022;
		do m=1 to 12;
			date=mdy(m,01,y);
			output;
		end;
	end;
	format date date9.;
run;

Alternatively, you could use the INTNX function to increment by one month from a chosen start date, and use a DO UNTIL loop to ensure you've reached your desired end date.

 

data want2;
	date="01jan2014"d; 
	output;
	do until (date>="01dec2022"d);
		date = intnx("MONTH",date,1,"BEGINNING");
		output;
	end;
	format date date9.;
run;

View solution in original post

4 REPLIES 4
antonbcristina
SAS Employee

Hi @NewUsrStat, it isn't immediately apparent what your end goal is here. Are you hoping to create separate datasets for each month (eg. a JAN dataset, a FEB dataset...). If you could please provide a sample input dataset and what you would like the output datasets to contain, along with code you've tried. 

NewUsrStat
Pyrite | Level 9
Hi thank you for your feedback! I need to create a single dataset with one column as I showed. Nothing more. I have no input data. I only have to create the dataset ex novo.
sbxkoenk
SAS Super FREQ
data work.abc;
 do i=0 to 100;
  maand = INTNX('MONTH','01JAN2014'd,i,'SAMEDAY');
  output;
 end;
 format maand MONYY5.;
run;

Koen

antonbcristina
SAS Employee

There's a few ways to achieve this. You could use DO loops to go through all of the years and months, and the MDY function to construct the date using the first of each month like so:

 

data want;
	do y=2014 to 2022;
		do m=1 to 12;
			date=mdy(m,01,y);
			output;
		end;
	end;
	format date date9.;
run;

Alternatively, you could use the INTNX function to increment by one month from a chosen start date, and use a DO UNTIL loop to ensure you've reached your desired end date.

 

data want2;
	date="01jan2014"d; 
	output;
	do until (date>="01dec2022"d);
		date = intnx("MONTH",date,1,"BEGINNING");
		output;
	end;
	format date date9.;
run;

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 417 views
  • 1 like
  • 3 in conversation