I have a date time field DATETIME20 which I converted to DTYEAR4. I would now like to use the year to give a flag of 0 or 1 if present. So for instance '7/23/2012 00:03:09'dt to 2012 to 2012 is present=1, else 0. Can I not just now use 2012 in the code?
I've used the following code but it doesn't work, thx.
data new;
format date dtyear4;
if date=2012 then flag=1; else flag =0
The DTYEAR format is used to print a datetime value as only the year. It does not change the value that is stored.
You can either compare the formatted value to the string value of the year or extract the year from the datetime value as number and compare it the numeric value of the year. So you could calculate your boolean FLAG variable this way.
flag = '2012' = put(date,dtyear4.) ;
or this way.
format date dtyear4.;
flag = '2012' = vvalue(date) ;
or this way.
flag = 2012 = year(datepart(date)) ;
No, the underlying value is still a datetime, a FORMAT just controls the appearance of the variable.
You can convert it using DATEPART and YEAR.
if year(datepart(variable)) = 2012 ...
The DTYEAR format is used to print a datetime value as only the year. It does not change the value that is stored.
You can either compare the formatted value to the string value of the year or extract the year from the datetime value as number and compare it the numeric value of the year. So you could calculate your boolean FLAG variable this way.
flag = '2012' = put(date,dtyear4.) ;
or this way.
format date dtyear4.;
flag = '2012' = vvalue(date) ;
or this way.
flag = 2012 = year(datepart(date)) ;
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.
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.