Hi all,
One quick question, in my data I have date values in YYYYMM format, how to read those values to display as YYYY-MM format;
here my values in raw data:
201809
200001
200205
201106
expecting output:
2018-09
2000-01
2002-05
2011-06
Thank you
-manoj
The answer depends on some of the information that you omitted, so there are a few variations. You will only need one of these solutions, depending on whether you are reading from raw data (vs. you already have a SAS data set), and depending on whether your existing variable is character or numeric. (If you had supplied this information, you wouldn't need to read through all the parts of the answer that you don't need.)
If you are reading those values originally from raw data, you can do it this way:
input dtvar yymmddn6.;
Only then can you print DTVAR using the yymmd7. format.
If you already have these values in a SAS data set, are they stored as numeric or as character?
If numeric, create a new variable in your desired format using:
length newvar $ 7;
newvar = put(dtvar, z6.);
newvar = put(input(newvar, yymmn6.), yymmd7.);
If character, create a new variable in your desired format using:
newvar = put(input(dtvar, yymmn6.), yymmd7.);
format YYMMD. should give you the display you are asking for.
The answer depends on some of the information that you omitted, so there are a few variations. You will only need one of these solutions, depending on whether you are reading from raw data (vs. you already have a SAS data set), and depending on whether your existing variable is character or numeric. (If you had supplied this information, you wouldn't need to read through all the parts of the answer that you don't need.)
If you are reading those values originally from raw data, you can do it this way:
input dtvar yymmddn6.;
Only then can you print DTVAR using the yymmd7. format.
If you already have these values in a SAS data set, are they stored as numeric or as character?
If numeric, create a new variable in your desired format using:
length newvar $ 7;
newvar = put(dtvar, z6.);
newvar = put(input(newvar, yymmn6.), yymmd7.);
If character, create a new variable in your desired format using:
newvar = put(input(dtvar, yymmn6.), yymmd7.);
HI @s_manoj "how to read those values to display as YYYY-MM format;"
What are the date values or can those date values revert back to first of the month?
Ok although answered,
/*Reading with yymmn6. will revert to 1st of the month*/
data have;
input dt : yymmn6.;
format dt yymmd.;*format with yymmd.;
cards;
201809
200001
200205
201106
;
Good question. I'm afraid I am not aware if there's a way to read directly to revert to last day of the month. But Intnx is always handy
/*Reading with yymmn6. will revert to 1st of the month*/
data have;
input dt : yymmn6.;
dt=intnx('month',dt,0,'e');
format dt yymmd.;*format with yymmd.;
cards;
201809
200001
200205
201106
;
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.