Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yyyymm convert to full sas date (dd/mm/ccyy) - SAS

Tags:

date

format

sas

I am trying to get the last day of the month from a field numeric in SAS (ccyymm).

for example 201401 would be 31-01-2014.

I have managed to get the field to show as a date type field (still showing as ccyymm though) with the following code in a PROC SQL SELECT statement

year_month_field INFORMAT YYMMN6. AS year_month_date

I think that I found some code from another question that had been asked that should give me the last day of the month once I can get the full date.

INTNX ( MONTH , year_month_date , 1) -1 

Will this work? If not any other suggestions would be appreciated.

Dan

like image 563
Dan Higginson Avatar asked Nov 30 '25 20:11

Dan Higginson


1 Answers

Note the difference between how one would convert a yyyymm to "last day of the month"MMMYYYY format below, depending on whether yyyymm is character or numeric variable.

data test;
yyyymm_character='201401';
yyyymm_numeric=201401;

date1=intnx('month', input(yyyymm_character, yymmn6.), 1)-1;
date2=intnx('month', input(put(yyyymm_numeric,6.), yymmn6.), 1)-1;
format date1 date2 date9.;
/*date1=date2=31jan2014*/
run;

Alternatively, you can use the in-built options for intnx functions to automatically set any input date to the last day of the respective month. Use 'e' as shown below.

data test;
yyyymm_character='201401';
yyyymm_numeric=201401;

date1=intnx('month', input(yyyymm_character, yymmn6.), 0, 'e');
date2=intnx('month', input(put(yyyymm_numeric,6.), yymmn6.), 0,'e');
format date1 date2 date9.;
/*date1=date2=31jan2014*/
run

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!