BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
user34567
Fluorite | Level 6

Hi,

In the query builder I added a computed column to add a year (2024) to the data set. But when I run the changes I get the output 1965 instead of 2024.

 

Computed item: PUT(YEAR(2024), 4.)

 

I really need it to be in date format and not a numeric format that's why I used the calculated item above.

 

What do I have to do differently?

 

Many thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@user34567 wrote:

Exactly, but I need it to be a date format YYYY, not numeric


A DATE is numeric.  But the number 2,024 is a day in July of 1965.  So applying the YEAR() function or the YEAR format to the value 2,024 will yield 1965.

 

Assign and actual date to your new variable.  Then attach a format, like YEAR4. that just displays the year part of the date value.

 

If VIYA supports SAS date constants use:

'01JAN2024'd

View solution in original post

12 REPLIES 12
PaigeMiller
Diamond | Level 26

If you have a variable whose value is the number 2024, you don't need to format it and you don't need to use it inside PUT(). You can simply use the variable which has the value of 2024.

 

Please explain further, show us the inputs and the desired output. What does it mean to "add a year to a data set"? SHOW US.

--
Paige Miller
user34567
Fluorite | Level 6

Hi @PaigeMiller, thank you for responding. If I just add "2024" it wel be marked as a numeric value instead of date. In SAS Viya I would like to link the years from two different datasets to eachother but I cannot do that when it is not seen as a real date.

LinusH
Tourmaline | Level 20
I'm confused, if I add "2024" in the calculated columns expression, a character variable is created.
Data never sleeps
user34567
Fluorite | Level 6

Exactly, but I need it to be a date format YYYY, not numeric

PaigeMiller
Diamond | Level 26

Stop referring to formats here. SHOW US what you have at the start and then SHOW US the desired end result.

--
Paige Miller
LinusH
Tourmaline | Level 20

????

Data never sleeps
Kurt_Bremser
Super User

@user34567 wrote:

Exactly, but I need it to be a date format YYYY, not numeric


A date in SAS is always numeric (count of days starting at 1960-10-01), which is displayed in human-readable form through a format of the date group.

The YEAR. format will display only the year part of such a value.

 

So, what do you have in both datasets (type and format of the existing variables), and along which value extracted from these do you want to join?

Tom
Super User Tom
Super User

@user34567 wrote:

Exactly, but I need it to be a date format YYYY, not numeric


A DATE is numeric.  But the number 2,024 is a day in July of 1965.  So applying the YEAR() function or the YEAR format to the value 2,024 will yield 1965.

 

Assign and actual date to your new variable.  Then attach a format, like YEAR4. that just displays the year part of the date value.

 

If VIYA supports SAS date constants use:

'01JAN2024'd
user34567
Fluorite | Level 6

Thank you, this was very helpful! Now I also understand why my 'solution' was not working.

Tom
Super User Tom
Super User

@user34567 wrote:

Hi @PaigeMiller, thank you for responding. If I just add "2024" it wel be marked as a numeric value instead of date. In SAS Viya I would like to link the years from two different datasets to eachother but I cannot do that when it is not seen as a real date.


If you want to create a DATE value you need to specify the month and the day also.  Normally people just use January 1st.

LinusH
Tourmaline | Level 20
If you need year as textual just add it as a constant in your expression:
"2024"
Data never sleeps
Reeza
Super User

@user34567 wrote:

Hi,

In the query builder I added a computed column to add a year (2024) to the data set. But when I run the changes I get the output 1965 instead of 2024.

 

Computed item: PUT(YEAR(2024), 4.)

 

I really need it to be in date format and not a numeric format that's why I used the calculated item above.

 

What do I have to do differently?

 

Many thanks in advance!


Do not use a calculated item.

Change the format on the variable to be YY4.  YEAR4. instead. 

https://documentation.sas.com/doc/da/vdmmlcdc/8.1/leforinforref/p0jwqba6i1r0j1n1xe9ovnn0pamr.htm

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 12 replies
  • 853 views
  • 0 likes
  • 6 in conversation