Hello -
I have imported a large dataset and the date column is formatted as follows: 2021-01-10. The dates run from 2021 through 2060.
I would like to be able to pull the respective elements from the date column and have them reported in another column. For instance, I would like to append three columns to my dataset labeled: year, month, day; and in those columns I would like to see 2021, 1, 10, respectively. I'm trying to figure out how to best do this and any suggestions would be much appreciated. Thanks!
Hi. Is the date value in the imported dataset a numeric SAS date value, or a character string? It's easy either way, though.
if character (assuming this is YYYY-MM-DD; otherwise, just flip the numbers around accordingly):
M = SUBSTR(DATEVAL,6,2) ; /* extract characters 6-7 */
D = SUBSTR(DATEVAL,9,2); /* extract characters 9-10 */
Y = SUBSTR(DATEVAL,1,4); /* extract characters 1-4 */
(and these could be converted to numeric values if needed).
If DATEVAL is a (formatted) numeric SAS date, there are functions to return date components as numeric values:
D = DAY(DATEVAL);
M = MONTH(DATEVAL);
Y = YEAR(DATEVAL);
Hi. Is the date value in the imported dataset a numeric SAS date value, or a character string? It's easy either way, though.
if character (assuming this is YYYY-MM-DD; otherwise, just flip the numbers around accordingly):
M = SUBSTR(DATEVAL,6,2) ; /* extract characters 6-7 */
D = SUBSTR(DATEVAL,9,2); /* extract characters 9-10 */
Y = SUBSTR(DATEVAL,1,4); /* extract characters 1-4 */
(and these could be converted to numeric values if needed).
If DATEVAL is a (formatted) numeric SAS date, there are functions to return date components as numeric values:
D = DAY(DATEVAL);
M = MONTH(DATEVAL);
Y = YEAR(DATEVAL);
Wonderful, thank you. The data is numeric, so that does make life vastly easier.
Do not work with human-readable dates as text strings and then try to pull them apart to obtain information. This is not a good approach. A better approach is to work with numeric SAS date values, which are the number of days since 01JAN1960, and are easy to create and easy to extract month and day and year. (And furthermore then you do NOT need separate variables that have month and day and year) SAS has done the hard work to program this, so you shouldn't have to.
But before we go any further, please tell us if the variable DATE is numeric or character, according to PROC CONTENTS. This is a critical piece of information before we go further.
Proc contents identifies the column as "num" and the format is YYMMDD10. Thanks.
@sas_user_1001 wrote:
Proc contents identifies the column as "num" and the format is YYMMDD10. Thanks.
Then use the YEAR(), MONTH() and DAY() functions.
What will you be doing with those other variables? For many purposes that people think they need the separate month, day or year for the actual date can be used.
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!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.