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;
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;
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;
It is great but may I ask why should it be inside the loop?
@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.
Proper visual formatting of the code (indentation!!!) would have made it easier for you to identify the mistake. See Maxim 12.
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!
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.