- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi SAS community.
I had posted a similar question here few years ago. I am facing the same issue and my old code does not address it.
Here is the problem:
I have a SAS file that I would like to save into CSV. Before that, I need the date format be easily read and understood under CSV. Currently, the date variable I have looks like
01NOV01 :00:00:00
The PROC CONTENTS command indicates the format to be DATETIME and the informat is ANYDTDTM40.
I would like to convert this to 20011101. And I have been trying variations of this code:
set want = datepart(have);
forma want yymmddn7.
where I replaced yymmddn7. with other formats. I either do not see any change to the variable or it just disappears.
I also tried
mydates = input(put(mydates, 8.), yymmdd8.);
format date
format mydates monyy7
still no results.
Any piece of advice if appreciated. Notice that I'm not a SAS nerd, I only use it everytime I need to load some big amount of financial data.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
set want = datepart(have);
forma want yymmddn7.
There is no SET command that does this in a SAS data step. The command is simply:
want=datepart(have);
Then you assign the proper format. You need 8 digits, so a format of 7 digits will not work. You also need to correct spelling and syntax errors in your FORMAT command.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
set want = datepart(have);
forma want yymmddn7.
There is no SET command that does this in a SAS data step. The command is simply:
want=datepart(have);
Then you assign the proper format. You need 8 digits, so a format of 7 digits will not work. You also need to correct spelling and syntax errors in your FORMAT command.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
thanks! it is now working well 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Length of 7 doesn't really makes sense, since you want 8?
Have you tried yymmddn8.?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
So a few points.
The variable you have does not have date values. It has datetime values. SAS stores dates as number of days and datetime as number of seconds. So to convert the value from a datetime value to a date value you need to divide by the number of seconds in a day. SAS provides a handy function, DATEPART(), that will do that for you.
If you want the value in the CSV file to be something that anyone looking at the CSV file will understand is a date then do not use a value like 20011101. That just looks like a number. Since there is no place in a CSV to store any metadata to indicate it isn't that number then most people (and programs) looking at the CSV file will assume it is just the number 20,011,101 instead of a date. It would be better to use something like 2001-11-01 instead. The hyphens will let the reader know it isn't a number. And displaying the date in YMD order will avoid the ambiguity caused by using either MDY or DMY order.
data for_export;
set have;
mydates=datepart(mydates);
format mydates yymmdd10.;
run;
proc export data=for_export file="want.csv" dbms=csv replace;
run;
And if actually have some consumer of the CSV file that requires that the dates be written in the file in that confusing YYYYMMDD style then you could leave the values as DATETIME values and just apply the E8601DN format to the variable.