Help using Base SAS procedures

SAS 9.4 Reformat dates after CSV import

Accepted Solution Solved
Reply
Contributor
Posts: 45
Accepted Solution

SAS 9.4 Reformat dates after CSV import

[ Edited ]

Hello,

I am importing a csv file from excel and the dates that is pulled from the file contains time as well.  SAS is reading the dates correctly, however I want to change the DATETIME16. to MMYYDD10.  Any guidance on how to do this would be greatly appreciated.  

Thank you,
Jeff S. O.


Accepted Solutions
Solution
‎04-06-2016 02:57 PM
Super User
Posts: 10,550

Re: SAS 9.4 Reformat dates after CSV import

If you use a datastep program to read CSV (highly recommended) instead of proc import then you specify the informat and format in the datastep.

 

If you are going to be repeatedly importing similar format files you are almost gauranteed to get some inconsistency over time from changing lengths of character variables, change from numeric to character, from date to character or similar. Getting one datastep and modifying input file and output dataset will save you lots of headaches down the road.

View solution in original post


All Replies
Super User
Posts: 6,977

Re: SAS 9.4 Reformat dates after CSV import

Do

newdate = datepart(date);
format newdate mmddyy10.;

(there is no mmyydd format)

If you want to preserve the original variable name, add

drop date;
rename newdate=date;

in the data step.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎04-06-2016 02:57 PM
Super User
Posts: 10,550

Re: SAS 9.4 Reformat dates after CSV import

If you use a datastep program to read CSV (highly recommended) instead of proc import then you specify the informat and format in the datastep.

 

If you are going to be repeatedly importing similar format files you are almost gauranteed to get some inconsistency over time from changing lengths of character variables, change from numeric to character, from date to character or similar. Getting one datastep and modifying input file and output dataset will save you lots of headaches down the road.

Contributor
Posts: 45

Re: SAS 9.4 Reformat dates after CSV import

Hello Ballardw,

Would you mind giving a brief example?  I have always used the import statement, but if I can decide the format before the dataset is created, that sounds way better than what I have been doing. 

 

Thank you,
Jeff S O

Super User
Posts: 10,550

Re: SAS 9.4 Reformat dates after CSV import

If you have been using Proc Import you should get code SAS generates to read a csv file in the log.

You can copy that and paste into the editor to see a basic example. Look at statements like INFORMAT. Those tell SAS how to read specific variables. When they are character the $nn sets the length of the text variables to nn characters.

 

Contributor
Posts: 45

Re: SAS 9.4 Reformat dates after CSV import

Thank you very much for that Ballardw!

Jeff S O

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 364 views
  • 0 likes
  • 3 in conversation