- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
This method Worked out.thanks much!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for the detailed explanation