- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Appreciate if someone of you help me understand to create the variable 'SDDTOP' and it should have the date value (last day of previous month) with character format.
e.g. SDDTOP=2018-03-31
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@Babloo wrote:
What if the value of 'have' is 2017-03-31 in numeric format and I just to
need to convert it to character? Can I use the put function with the
format $10.?
I would have thought you understand in-between how SAS Date and DateTime values work. If the value is numeric then it's either the count of days (date value) or seconds (datetime value) since 1/1/1960. You then apply a format to make this value human readable as a date/datetime value. This doesn't change the value itself which just remains a number, the format is just for printing.
If you really want to store such a numeric value representing a data/datetime in a character variable, then you have to use the put() function. This function does nothing else than applying the format you use to your numeric value and then write the result as a string - and if this is on the right side from the equal sign then you're assigning this string value to a character variable. In other languages the comparable function to a SAS put() is called write() - and read() for input().
Can I use the put function with the format $10.?
I hope you understand now that the answer to this question is "no, of course not". You have to use the appropriate date/datetime format to write the number as a date/datetime string.
And last but not least: It's really MUCH better to keep dates as SAS date/datetime values (=numeric) with a format permanently assigned to the variable for printing/looking at it. Keeping the dates numeric allows you to actually use such data (i.e. if you want to calculated the number of days between two dates and the like; or even if you just want to sort by date).
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Use the intnx function and a yymmddd10. format.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I already tried this, but it is in numeric format. I want it to be in character format.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I tried this code and end up with the invalid values like ******** for the variable b and c.
data test;
a='31JAN2017:00:00:00'dt;
format a datetime23.;
b=PUT(a,date9.);
c=put(a,YYMMDD10.);
run;
My objective is to create two new variable based on the variable 'a'. One variable (e.g. 'b') should have value the 31JAN2017 in numeric format and another one (e.g. 'c') should have the value 2017-01-31 and it should be in character format.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
So you do not have a DATE value, as you told lyingly to us, but a DATETIME value. As you were told probably a hundred times now, a date value is a count of DAYS, while a datetime value is a count of SECONDS, so a datetime value is MUCH too big for a date format. There is a magic function called DATEPART that extracts the date out of a datetime. USE IT.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I managed to create one of the two variables using datepart function. However I'm seeking your help to create the variable and value as below (last day of previous month) and it should be in character format. Challenge which i have here is to make this as a character variable.
SDDTOP = 2018-03-31
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
put() function converts numeric to character.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data test; a='31JAN2017:00:00:00'dt; format a datetime23.; b=put(datepart(a),date9.); c=put(datepart(a),yymmdd10.); run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@Babloo wrote:
I managed to create one of the two variables using datepart function. However I'm seeking your help to create the variable and value as below (last day of previous month) and it should be in character format. Challenge which i have here is to make this as a character variable.
SDDTOP = 2018-03-31
The way I went about this:
1. Google search: site:support.sas.com 9.4 formats and informats
That returns as the very first hit: https://support.sas.com/documentation/cdl/en/leforinforref/64790/HTML/default/viewer.htm
2. Go to the documentation for Formats
3. Open up "Formats by category"
4. search for yyyy-
....and this then led me to format e8601dn. which returns exactly what you're asking for without even the need to first convert your SAS datetime to a date value.
data test;
have='31JAN2017:00:00:00'dt;
format have datetime23.;
want=put(have,e8601dn.);
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
need to convert it to character? Can I use the put function with the
format $10.?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Well, 2017-03-31 gives an interesting numeric value:
data test;
x1 = 2017-03-31;
run;
proc print data=test;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@Babloo wrote:
What if the value of 'have' is 2017-03-31 in numeric format and I just to
need to convert it to character? Can I use the put function with the
format $10.?
I would have thought you understand in-between how SAS Date and DateTime values work. If the value is numeric then it's either the count of days (date value) or seconds (datetime value) since 1/1/1960. You then apply a format to make this value human readable as a date/datetime value. This doesn't change the value itself which just remains a number, the format is just for printing.
If you really want to store such a numeric value representing a data/datetime in a character variable, then you have to use the put() function. This function does nothing else than applying the format you use to your numeric value and then write the result as a string - and if this is on the right side from the equal sign then you're assigning this string value to a character variable. In other languages the comparable function to a SAS put() is called write() - and read() for input().
Can I use the put function with the format $10.?
I hope you understand now that the answer to this question is "no, of course not". You have to use the appropriate date/datetime format to write the number as a date/datetime string.
And last but not least: It's really MUCH better to keep dates as SAS date/datetime values (=numeric) with a format permanently assigned to the variable for printing/looking at it. Keeping the dates numeric allows you to actually use such data (i.e. if you want to calculated the number of days between two dates and the like; or even if you just want to sort by date).