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. thanks a lot.


Accepted Solutions
Solution
‎07-19-2017 12:57 PM
Super User
Posts: 6,946

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

You can use the yymm format in a put statement.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎07-19-2017 12:57 PM
Super User
Posts: 6,946

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

You can use the yymm format in a put statement.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 97

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

just like this? but it didn't work.

ym=put(yymm,yymm6.);
Frequent Contributor
Posts: 97

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

sorry , i make a mistake. it has been resolved using the code. thank you!

Frequent Contributor
Posts: 97

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

but after  finished, it's  character  format instead of date format. my code :

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;
Super User
Posts: 6,946

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

[ Edited ]

lixuan wrote:

but after  finished, it's  character  format instead of date format. my code :

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;

A SAS data value defines a certain day. When you only have year and month, one usually uses a character variable for display, as a SAS data variable cannot be built without a day value.

If you simply assign the yymm format to your original variable, it will display the way you want to. If you do want to create a new SAS date variable, you will have to set a rule for the day that it will use "internally", and then assign the yymm format to the new variable.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 106 views
  • 0 likes
  • 2 in conversation