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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.);

 

View solution in original post

10 REPLIES 10
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

format YYMMD. should give you the display you are asking for.

 

Astounding
PROC Star

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.);

 

s_manoj
Quartz | Level 8
hi @Astounding, thank you for covering for all possible situations,
yes, I am reading date values from a sas dataset in character form
novinosrin
Tourmaline | Level 20

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?

s_manoj
Quartz | Level 8
Hi @novinosrin
yes, can revert back to first of month
novinosrin
Tourmaline | Level 20

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
;
s_manoj
Quartz | Level 8
Thank you
s_manoj
Quartz | Level 8
How to read , if we want to display with last day of month?
novinosrin
Tourmaline | Level 20

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
; 

 

s_manoj
Quartz | Level 8
Thank you @novinosrin
its informative

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