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

In a dataset I have a date values in the variable assign_date=01MAR2022:00:00:00 .I Need only 01MAR2022 in the variable.can anyone please help?.

 

I tried to use substr(assign_date,1,9) but it gave some sas value date.-any reason why so?

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

If this is the same data as in your earlier thread, the variable is numeric. You can't use SUBSTR on numeric variables. SUBSTR only works on character variables.

 

Use this:

assign_date=datepart(assign_date);
format assign_date date9.; /* or any other date format you want */
--
Paige Miller

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

In SAS, anything that has a time element cannot be a date. SAS dates are counts of days, times and datetimes are counts of seconds.

To extract the date from a SAS datetime, you must use the DATEPART function.

PaigeMiller
Diamond | Level 26

If this is the same data as in your earlier thread, the variable is numeric. You can't use SUBSTR on numeric variables. SUBSTR only works on character variables.

 

Use this:

assign_date=datepart(assign_date);
format assign_date date9.; /* or any other date format you want */
--
Paige Miller
Akshaya_1397
Obsidian | Level 7

This method Worked out.thanks much!

ballardw
Super User

If the value is a SAS datetime value then "substr" would ask SAS to convert the underlying datetime value (which is typically a moderately large number of seconds) to character so that SUBSTR can be applied as shown in this log:

1    data example;
2       x="01MAR2022:00:00:00"dt;
3       y=substr(x,1,9);
4    run;

NOTE: Numeric values have been converted to character values at the places given by:
      (Line):(Column).
      3:13

Which has a result of 1961712, which are the first 9 characters of the value 1961712000 which is the number of seconds since midnight 01JAN1960 (or 01Jan1960:00:00:00).

It appears that your value is a DATETIME value (type is numeric and the current format assigned to the variable is DATETIME19. (or longer).

You want the DATEPART function to extract the value AND to show the value as a date assign a different format such as DATE9

 

data example;
   x="01MAR2022:00:00:00"dt;
   put x= datetime18.;
   x= datepart(x);
   put x= date9.;
run;

If you do not change the format you will likely see "dates" that appear as similar to 01JAN1960:06:18:25

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

Akshaya_1397
Obsidian | Level 7

Thanks for the detailed explanation 

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