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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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