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 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 403 views
  • 1 like
  • 4 in conversation