BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sasphd
Lapis Lazuli | Level 10

hello 

I want to transform date format to year


data _7_Morningstar_Data2_expense;
set _7_Morningstar_Data_expense;
sas_date = input(put(date, 40.), YEAR.);
format sas_date YEAR.;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@sasphd wrote:

no date is $40. format is not a date in SAS file I want to transform it to date format and it is in year like 1984; 1985 etc


A date format would imply there is a year, month and day; but if you only have year, then just leave it as an integer. No formatting needed.

 

sas_date = put(strip(date), 4.);
--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Is variable DATE a true SAS date value? If so, all you need to do is assign format YEAR. to this variable.

 

What is the variable type and format according to PROC CONTENTS? Is it numeric? Or is it character? What format does it have? What are typical values?

 

 

--
Paige Miller
sasphd
Lapis Lazuli | Level 10

no date is $40. format is not a date in SAS file I want to transform it to date format and it is in year like 1984; 1985 etc

PaigeMiller
Diamond | Level 26

@sasphd wrote:

no date is $40. format is not a date in SAS file I want to transform it to date format and it is in year like 1984; 1985 etc


A date format would imply there is a year, month and day; but if you only have year, then just leave it as an integer. No formatting needed.

 

sas_date = put(strip(date), 4.);
--
Paige Miller
Tom
Super User Tom
Super User

So you have character variable that could have string up to 40 bytes long?

And you want to convert that into a number that represents a year?

Hopefully most of the values do not contain strings that are longer than 4 characters otherwise I am not sure what a year that large would mean.

The INPUT() function is what you need to convert character strings into numbers.  The maximum length the numeric informat can handle is 32 bytes.

year=input(left(date),32.);

If you want to treat that as an actual date then you will need to pick some specific day within that year, such as January first.

If you want to display just the year from this calculated date value you could attach the YEAR format to it.

sas_date=mdy(1,1,year);
format sas_date year4.;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 4 replies
  • 855 views
  • 1 like
  • 3 in conversation