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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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