BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jsandsk
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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)) ;

 

View solution in original post

2 REPLIES 2
Reeza
Super User

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 ...
Tom
Super User Tom
Super User

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)) ;

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 3119 views
  • 0 likes
  • 3 in conversation