BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ronein
Onyx | Level 15

Hello

I am creating series of sas dates .

I want to create a character column with structure YYMM .

What is wrong that I get null values ?

As I known  to convert sas date to char date need to use PUT function

/*Example1*/
%let start_date=01May2019;
%let end_date=01NOV2019;
data want_month1;
date_sas="&start_date"d;
do while (date_sas<="&end_date"d);
output;
date_sas=intnx('month', date_sas, 1, 's');
end;
format date_sas yymmn4.;
date_char=put(date_sas,yymmn4.);
run;


/*Example2*/
%let start=1905;
%let end=1911;
data want_month2;
  date_start=mdy(mod(&start,100),1,floor(&start/100));
  date_end=mdy(mod(&end,100),1,floor(&end/100));

date_sas=date_start;
do while (date_sas<=date_end);
output;
date_sas=intnx('month', date_sas, 1, 's');
end;
format date_sas yymmn4.;
date_char=put(date_sas,yymmn4.);
run;
1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

Put the assignment to DATE_CHAR inside the loop, that should help:

/*Example1*/
%let start_date=01May2019;
%let end_date=01NOV2019;
data want_month1;
  date_sas="&start_date"d;
  do while (date_sas<="&end_date"d);
    date_char=put(date_sas,yymmn4.);
    output;
    date_sas=intnx('month', date_sas, 1, 's');
    end;
format date_sas yymmn4.;
run;

View solution in original post

5 REPLIES 5
geoskiad
Fluorite | Level 6
Hi Ronein,

Try placing the date_sas, date_char and format assignment inside the do loop and before the output statement.
s_lassen
Meteorite | Level 14

Put the assignment to DATE_CHAR inside the loop, that should help:

/*Example1*/
%let start_date=01May2019;
%let end_date=01NOV2019;
data want_month1;
  date_sas="&start_date"d;
  do while (date_sas<="&end_date"d);
    date_char=put(date_sas,yymmn4.);
    output;
    date_sas=intnx('month', date_sas, 1, 's');
    end;
format date_sas yymmn4.;
run;
Ronein
Onyx | Level 15

It is great but may I ask why should it be inside the loop?

Kurt_Bremser
Super User

@Ronein wrote:

It is great but may I ask why should it be inside the loop?


Because it otherwise does not affect the output, which is in the loop. The explicit output statement disables the implicit output that SAS would normally compile at the end of the data step iteration.

Anything that is intended to have an effect on the expected output needs to happen before the output statement, naturally. And at the same frequency.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 5 replies
  • 1950 views
  • 3 likes
  • 4 in conversation