Lorem Ipsum
Lorem Ipsum

Reputation: 4534

SAS: Get current year in YY format

I want to assign the current year in a YY format to either a macro or data set variable.

I am able to use the automatic macro variables &sysdate or &sysdate9 to get the current date. However, extracting the year in a YY format is proving to be a nightmare. Below are some examples of what I've been trying.

There exists the YEARw. format. But when I try to use it I get errors or weird results. For instance, running

data _null_;
  yy = year(input("&sysdate9.", year2.));
  put  yy=;
run;

produces the error

ERROR 48-59: The informat YEAR was not found or could not be loaded.

If I try to format the variable in the output, I get 1965 instead of the current year. The following

data _null_;
  yy = year(input("&sysdate9.", date9.));
  put  yy= yy year2.;
run;

outputs

yy=2016 65

Please help.

Upvotes: 3

Views: 6371

Answers (3)

Reeza
Reeza

Reputation: 21274

  • Apply the year function to the date variable
  • Convert to string
  • Take last 2 digits

EDIT: change input to PUT

    Year = substr(put(year(today()), 4.), 3);

Upvotes: 0

Joe
Joe

Reputation: 63424

I think it is worth exploring the issues here in more detail.

First, Formats are patterns for converting numeric values to a human readable format. That's what you want to do here: convert a date value to a human readable format, in this case to a year.

Informats, on the other hand, convert human readable information to numeric values. That's not what you're doing here; you have a value already.

Second, put matches with Formats, and input matches with informats, exclusively.

Third, you get close in your last try: but you misuse the year format. Formats are basically value mappings, so they map every possible numeric value in their range (sometimes "all values" is the range, sometimes not) to a display value (string). You need to know what kind of value is expected on the input. YEARw. expects a date value as input, not a year value: meaning input is "number of days from 1/1/1960", mapped to "year". So you cannot take a value you've already mapped to a year value and map it again with that method; it will not make any sense.

Let's look at it:

data _null_;
  yy = year(input("&sysdate9.", date9.));
  put  yy= yy year2.;
run;

yy contains the result of the year function - 2016. Good so far. Now, you need the 2 digit year (16); you can get that through mod function, if you like, or put/substr/input:

data _null_;
  yy = input(substr(put(year(input("&sysdate9.", date9.)),4.),3,2),2.);
  put yy=;
run;

mod is probably easier though since it's a number. But of course you could've used year:

data _null_;
  yy = put(input("&sysdate9.", date9.),year2.);
  put yy=;
run;

Now, yy is character, so you could wrap that with input(...,2.) or leave it character depending on your purposes.

Finally - a use note on &sysdate9.. You can easily make this a date without input:

"&sysdate9."d

So:

yy = put("&sysdate9."d,year2.);

That's called a date literal (and "..."dt and "..."t also work for datetime,time). They require things in the standard SAS formats to work properly.

And as pointed out in Nicarus' answer, today() is a bit better than &sysdate9 since it is guaranteed to be today. If you're running this in batch or restart your session daily, this won't matter, but it will if you have a long-running session.

Upvotes: 3

Nick
Nick

Reputation: 7451

This works to get you the 2-digit year number of the current year:

DATA _NULL_;

YEAR = PUT(TODAY(),YEAR2.);
PUT YEAR;

RUN;
/* Returns: 16 */

To breakdown what I am doing here:

I use TODAY() to get the current date as a DATE type. &SASDATE needs to be converted to a DATE, but also it is the date that the SAS session started. TODAY() is the current date.

PUT allows us to pass in a non-character (numeric/date) value, which is why it is used with TODAY() as opposed to INPUT.

Upvotes: 4

Related Questions