BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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!
3 REPLIES 3
Peter_C
Rhodochrosite | Level 12
use the year format with length 2, like[pre] put @4 date year2. ;[/pre]
PeterC
Doc_Duke
Rhodochrosite | Level 12
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).
Peter_C
Rhodochrosite | Level 12
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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1488 views
  • 0 likes
  • 3 in conversation