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: 7,808

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: 7,808

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?

Posted in reply to KurtBremser

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: 7,808

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
New Contributor
Posts: 4

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

, I have input file having year ( let's say 2015 to 2017 YYYY) .
I am trying to fetch system year ( year(today())
And then compare year from input file to fetched year.
If matches , those rows should come in output.

If comparison for years is not working ..Have tried reading input year in chat..Integer both..But nothing working.
Can some one please help..Urgent
Super User
Posts: 7,808

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

[ Edited ]

Singla14 wrote:
, I have input file having year ( let's say 2015 to 2017 YYYY) .
I am trying to fetch system year ( year(today())
And then compare year from input file to fetched year.
If matches , those rows should come in output.

If comparison for years is not working ..Have tried reading input year in chat..Integer both..But nothing working.
Can some one please help..Urgent


Don't hijack other people's threads, start your own.

 

Post your code and the log of the whole step.

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

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

Discussion stats
  • 7 replies
  • 254 views
  • 0 likes
  • 3 in conversation