DATA Step, Macro, Functions and more

Getting the YY only from DATE()

Reply
N/A
Posts: 0

Getting the YY only from DATE()

Hi all

I am trying to get the YY part of the YEAR() function and write it out to a dataset but cannot get this done.

This is my code:

DATA XXX;
YR = YEAR(DATE());
FORMAT YR $04.;
YRF = SUBSTR(YR,3,2);

FILE MERGED;
PUT @001 RECORDH $01.
@002 MTH 2.
@004 YRF $02.
;

But this only writes out a blank - what am i missing?

Thanks!
Valued Guide
Posts: 2,177

Re: Getting the YY only from DATE()

Posted in reply to deleted_user
use the year format with length 2, like[pre] put @4 date year2. ;[/pre]
PeterC
Trusted Advisor
Posts: 2,116

Re: Getting the YY only from DATE()

Posted in reply to deleted_user
Peter has a solution, but your problem is that you have mixed up numeric and character data types. You should have gotten lots of warnings about converting numeric to character or vice-versa.

The YEAR function returns a numeric value. If you rewrote as

DATA XXX;
LENGTH yrf $ 2;
YRF = PUT(YEAR(DATE()),year2.);

FILE MERGED;
PUT @001 RECORDH $01.
@002 MTH 2.
@004 YRF $02.
;

it should work (though you are missing other parts of your code).
Valued Guide
Posts: 2,177

Re: Getting the YY only from DATE()

needs minor revision (cannot apply the year format to the value returned by the year() function)[pre]YRF = PUT(DATEvariable,year2.);[/pre] But that dateVariable could be formatted in the put statement, since the string variable is not used elsewhere.
I would not use this routine with the date function, but that is just my personal preference. I don't often have to prepare programs to run where date() might return a different year from the year in constant &sysdate. (Does one of the BI servers create that situation?)
PeterC
Ask a Question
Discussion stats
  • 3 replies
  • 124 views
  • 0 likes
  • 3 in conversation