DATA Step, Macro, Functions and more

How to extract year and month from date in SAS?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 97
Accepted Solution

How to extract year and month from date in SAS?

 I have a variable in the form of yymmdd, and I want to extract yymm from it without changing the date format. I konw year() and month(),but I don't know how to get year and month in one variable. I asked the question just now and got the answer of using put to resolve it, but after use the method, i found the format is character. Here is my code. thanks a lot.

data have;
format dt yymmdd10.;
input dt yymmdd10.;
datalines;
2016-05-21
2017-08-12
2014-03-02
;
run;

data want;
set have;
yy = put(dt,yymmn6.);
run;

 


Accepted Solutions
Solution
‎07-19-2017 03:30 PM
Super User
Super User
Posts: 6,502

Re: How to extract year and month from date in SAS?

[ Edited ]

lixuan wrote:

Thanks, if I wnna get a new variable of yymm in date format, how can I do?


You could copy the date into another variable and attach a different format.

newdate= olddate ;
format olddate yymmdd10. newdate yymmn6. ;

Otherwise you will need to convert to something other than a date, since an actual date value requires a day of the month.

newdate_ch = put(olddate,yymmn6.);
newdate_num = input(put(olddate,yymmn6.),6.);
* or ;
newdate_num = year(olddate)*100 + month(olddate);

 

View solution in original post


All Replies
Super User
Posts: 17,840

Re: How to extract year and month from date in SAS?

 

Apply the FORMAT don't convert it.

 

format dt yymmn6.;

 

Frequent Contributor
Posts: 97

Re: How to extract year and month from date in SAS?

Thanks, if I wnna get a new variable of yymm in date format, how can I do?

PROC Star
Posts: 7,363

Re: How to extract year and month from date in SAS?

Just input the result of your put. e.g.:

data want;
  set have;
  yy = input(put(dt,yymmn6.),8.);
run;

Art, CEO, AnalystFinder.com

 

Frequent Contributor
Posts: 97

Re: How to extract year and month from date in SAS?

Ok, Thank you . Combining with both your solvements, I get the answer,  however I wonder if there is a simpler way ?

data want;
  set have;
  yy = input(put(dt,yymmn6.),8.);
  format yy yymmn6.;
run;
Super User
Super User
Posts: 6,502

Re: How to extract year and month from date in SAS?


lixuan wrote:

Ok, Thank you . Combining with both your solvements, I get the answer,  however I wonder if there is a simpler way ?

data want;
  set have;
  yy = input(put(dt,yymmn6.),8.);
  format yy yymmn6.;
run;

You cannot apply the YYMMN format to a number like 201,707.  SAS stores dates as the number of days since start of 1960 so something like 201,707 would be in the year 2512, not in the year 2017.

Frequent Contributor
Posts: 97

Re: How to extract year and month from date in SAS?

Yes, I check again and find that i am wrong. Thank you .

Super User
Posts: 17,840

Re: How to extract year and month from date in SAS?

Another option is to duplicate the variable and then apply the format to the new variable. You can also apply formats in specific locations to override whats in your data. For example, if you want to create a report thats at the monthly level but only have data, you can apply the format in PROC REPORT or MEANS/TABULATE and it will aggregate correctly. 

 

 

Solution
‎07-19-2017 03:30 PM
Super User
Super User
Posts: 6,502

Re: How to extract year and month from date in SAS?

[ Edited ]

lixuan wrote:

Thanks, if I wnna get a new variable of yymm in date format, how can I do?


You could copy the date into another variable and attach a different format.

newdate= olddate ;
format olddate yymmdd10. newdate yymmn6. ;

Otherwise you will need to convert to something other than a date, since an actual date value requires a day of the month.

newdate_ch = put(olddate,yymmn6.);
newdate_num = input(put(olddate,yymmn6.),6.);
* or ;
newdate_num = year(olddate)*100 + month(olddate);

 

Frequent Contributor
Posts: 97

Re: How to extract year and month from date in SAS?

OK, I get it. Thank you all very much.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 94 views
  • 0 likes
  • 4 in conversation