BookmarkSubscribeRSS Feed
Nieves
Quartz | Level 8

Hi SAS expert, I have a date in the format of yyyymm, how to format in SAS date ?

yyyymm

196507

196508

196509

196510

196511

196512

196601

196602

196603

196604

196605

Thanks

7 REPLIES 7
Patrick
Opal | Level 21

The informat instructs SAS how to read a text string for conversion to a SAS Date value, the format instructs SAS how to print the SAS Date value.

The SAS Date value created will be the first of the month. You can use another format like DATE9. to also print the date. 

data sample;
  attrib my_dt length=8 informat=yymmn6. format=yymmn6.;
  input my_dt;
  datalines;
196507
196508
196509
196510
196511
196512
196601
196602
196603
196604
196605
;

proc print data=sample;
run;

proc print data=sample;
  format my_dt date9.;
run;

Patrick_0-1671964110925.png

 

Nieves
Quartz | Level 8
Thank you very much. By the way, if I would like to generate two date variables, first day of one month after the yyyymm and last day of one month after the yyyymm, how shall I implement this ?

yyyymm date1 date2
196507 19650801 19650831
196508 19650901 19650930
196509 19651001 19761031
Nieves
Quartz | Level 8
Thanks. If I modify the code as follows, there seems an error. May I seek your advice ?
data syy.data2;
set syy.data1;
attrib yyyymm length=8 informat=yymmn6. format=yymmn6.;
run;


data syy.data3;
set syy.data2;
format yyyymm date9.;
run;
Kurt_Bremser
Super User
  1. this is not my code
  2. whenever you have errors or other messages you can't make sense of, post the complete log of the step into a window opened with this button:Bildschirmfoto 2020-04-07 um 08.32.59.jpg
Nieves
Quartz | Level 8
Thanks. If I modify the code as follows, there seems an error. May I seek your advice ?
data syy.data2;
set syy.data1;
attrib yyyymm length=8 informat=yymmn6. format=yymmn6.;
run;


data syy.data3;
set syy.data2;
format yyyymm date9.;
run;
Tom
Super User Tom
Super User

Attaching a format to an existing variable does not change the values stored in the variable.

So if you have a numeric variable that has a value like 196,507 and tell SAS to use the YYMMN6. format to display it then it will treat that number as the number of days since 1960 so that number would mean  06JAN2498.

 

If you want to convert the number 196,507 to the date 01JUL1965 then you need to convert it.  Since INFORMATs convert text to numbers you first need to use a FORMAT to convert the number to a text string.

data syy.data2;
  set syy.data1;
  yyyymm = input(put(yyyymm,z6.),yymmn6.);
  format yyyymm date9.;
run;

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
  • 7 replies
  • 833 views
  • 1 like
  • 4 in conversation