BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
JKCho
Pyrite | Level 9
length year $ 20;
year=substr(caldt,1,length(caldt)-4;
run;

This is the code I used but I am very sure it does not work....

 

1.PNG

 

When I used the code above, it gives me seemingly non-random numbers but not date. like 8778 8782...

Since I have no idea about subtracting across different formats(I know across formats it needs different codes..), I tried many codes but all gave me weird numbers...

 

What I simply want to have is the YEAR part. for example, 19880106 -> 1988

Currently, the original variable's format is YYMMDDN8. and when I use codes I have used so far... the format changed to 8. or 12.

 

How can I simply get the YEAR part only? 

I appreciate your help a lot!!!!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

So your PROC CONTENTS confirms what I suspected, and the YEAR function is all you need to obtain the year.


Remember that a formatted date number is still internally to SAS as an integer (which is the number of days since January 1, 1960). It doesn't really matter that us humans see 19840103, SAS thinks it is 8768 and the YEAR function will obtain the year from it. It is harder (and not really worth doing) to extract the year from the formatted number.

--
Paige Miller

View solution in original post

9 REPLIES 9
Reeza
Super User
Did you check your log? I guarantee there's errors in there. Is your variable numeric or character?

If character, try SUBSTRN () - note the N instead.

year_char = substrn(variable, 1, 4);
year_num = input(year_char, 4.);
PaigeMiller
Diamond | Level 26

It would appear that your variable CALDT is an actual integer containing a valid SAS date and formatted with some format that makes it appear to us humans as 19840103. Can you confirm this by running PROC CONTENTS on this data set?

 

In this case, to get the year is simple:

 

year=year(caldt);

In any event, it is highly advised to learn how to treat SAS dates and SAS datetimes and SAS times as numbers (you seem to be treating them as text strings), and so you also need to learn about how to format these date and time and datetime numbers, and how to use simple functions such as YEAR() and MONTH() and INTCK() and INTNX(). If you are going to be dealing with dates in your programs, this seems mandatory to me.

--
Paige Miller
JKCho
Pyrite | Level 9

I agree.. I have learned statistical analysis using SAS but nothing about data trimming and it is now so demanding.

I am not sure what to tell you... 

 

After PROC CONTENTS. I can see below. CALDT is number in YYMMDD format. 

 

Alphabetic List of Variables and Attributes# Variable Type Len Format Informat Label12
CALDTNum8YYMMDDN8.YYMMDD8.Calendar Date
SPINDXNum88.28.2Level of the S&P 500 Index
PaigeMiller
Diamond | Level 26

So your PROC CONTENTS confirms what I suspected, and the YEAR function is all you need to obtain the year.


Remember that a formatted date number is still internally to SAS as an integer (which is the number of days since January 1, 1960). It doesn't really matter that us humans see 19840103, SAS thinks it is 8768 and the YEAR function will obtain the year from it. It is harder (and not really worth doing) to extract the year from the formatted number.

--
Paige Miller
JKCho
Pyrite | Level 9
Thank you. the simple year=year(caldt); code works!
It is like a magic!
Kurt_Bremser
Super User

@JKCho wrote:

I agree.. I have learned statistical analysis using SAS but nothing about data trimming and it is now so demanding.

I am not sure what to tell you... 

 

After PROC CONTENTS. I can see below. CALDT is number in YYMMDD format. 

 

Alphabetic List of Variables and Attributes# Variable Type Len Format Informat Label12
CALDT Num 8 YYMMDDN8. YYMMDD8. Calendar Date
SPINDX Num 8 8.2 8.2 Level of the S&P 500 Index

As already stated, use the year() function to extract the year from the date.

JKCho
Pyrite | Level 9
Thank you!
Reeza
Super User
Probably more in depth than you want.

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

If your variable is numeric, Paigemiller's solution is your best option.
JKCho
Pyrite | Level 9
Thank you for sharing the link!
I have to look into it crefully!

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
  • 9 replies
  • 1101 views
  • 6 likes
  • 4 in conversation