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

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
Meteorite | Level 14

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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 747 views
  • 3 likes
  • 4 in conversation