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....
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!!!!
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.
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.
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.
CALDT | Num | 8 | YYMMDDN8. | YYMMDD8. | Calendar Date |
SPINDX | Num | 8 | 8.2 | 8.2 | Level of the S&P 500 Index |
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.
@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.
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!
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.