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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
davehalltwp
Quartz | Level 8

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);




View solution in original post

6 REPLIES 6
davehalltwp
Quartz | Level 8

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);




sas_user_1001
Obsidian | Level 7

Wonderful, thank you. The data is numeric, so that does make life vastly easier.

PaigeMiller
Diamond | Level 26

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.

 

 

--
Paige Miller
sas_user_1001
Obsidian | Level 7

Proc contents identifies the column as "num" and the format is YYMMDD10. Thanks.

 
Tom
Super User Tom
Super User

@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.

ballardw
Super User

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.

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!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 6 replies
  • 905 views
  • 3 likes
  • 5 in conversation