SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ali11
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
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

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26
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
ali11
Fluorite | Level 6

thanks! it is now working well 🙂

LinusH
Tourmaline | Level 20

Length of 7 doesn't really makes sense, since you want 8?

Have you tried yymmddn8.?

Data never sleeps
Tom
Super User Tom
Super User

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.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 4 replies
  • 1513 views
  • 2 likes
  • 4 in conversation