Help using Base SAS procedures

How to format dates in SAS

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

How to format dates in SAS

I'm very new to SAS and I am trying to import a CSV file with a variable that has dates. When I import it in it looks fine (in the correct format). However when I am trying to create a new variable it does not recognize the dates. I'm currently using SAS 9.3. 

 

For example: 

 

PROC IMPORT DATAFILE= "S:\eSTAR\NDSR Data\NDSR_6_21_16.csv"
out=NDSR
dbms=csv
replace;
GETNAMES=YES;
run;

 

DATA ndsr; set ndsr;
format date Date9.;
run;

 

data ndsr_new; set ndsr;

if id=201 AND date=7/9/2015then flag=1;

run;

 

When I run this syntax it creates the variable flag however it doesn't recognize the date I entered it and so therefore it shows as missing. I have tried to change the format of the date variable to other date formats by SAS (e.g. DDMMYYD10.) 

 

Any help would be appreciated! 

 

 


Accepted Solutions
Solution
‎06-22-2016 11:26 AM
Super Contributor
Posts: 441

Re: How to format dates in SAS

[ Edited ]

Hi,

 

Welcome to the wonderful world of SAS!

 

SAS stores dates internally as integers indicating the number of days since 1 january 1960. So your line

 

if id=201 AND date=7/9/2015 then flag=1;

couildn't possibly work, even if you added quotes around the value. What you must do is use the date literal notation. This is the date in DATE9. format (DATE7. will also work) between quotes and a d behind it:

 

 

if id=201 AND date='7SEP2015'd then flag=1;

This assumes that the import has succesfully interpreted your data as being dates. Run proc contents on dataset NDSR to know for sure. But I am pretty confident this will be the way. And if not you may be able to use this little gem of knowledge in the near future ;-).

 

Hope this helps,

 - Jan.

View solution in original post


All Replies
Solution
‎06-22-2016 11:26 AM
Super Contributor
Posts: 441

Re: How to format dates in SAS

[ Edited ]

Hi,

 

Welcome to the wonderful world of SAS!

 

SAS stores dates internally as integers indicating the number of days since 1 january 1960. So your line

 

if id=201 AND date=7/9/2015 then flag=1;

couildn't possibly work, even if you added quotes around the value. What you must do is use the date literal notation. This is the date in DATE9. format (DATE7. will also work) between quotes and a d behind it:

 

 

if id=201 AND date='7SEP2015'd then flag=1;

This assumes that the import has succesfully interpreted your data as being dates. Run proc contents on dataset NDSR to know for sure. But I am pretty confident this will be the way. And if not you may be able to use this little gem of knowledge in the near future ;-).

 

Hope this helps,

 - Jan.

New Contributor
Posts: 2

Re: How to format dates in SAS

Posted in reply to jklaverstijn

Thanks Jan! It worked Smiley Happy 

Super User
Super User
Posts: 7,977

Re: How to format dates in SAS

Well, you halfway to a good import process, you are using a good datafile format, so congrats.  To support this I would recommend you drop proc import, which is a guessing procedure.  It looks at your data and tries to guess what the best format is.  This is not a good idea.  You are the person who knows your data (and if you don't why?) so you would be best placed to write the code which imports the data.  Have a look at the documentation for importing text files, but something like:

data want;
  infile "<yourfile>.csv";
  label <variables and labels>;
  informat <variables and informats>;
  format <variables and formats>;
  length <variables and lengths>;
  input <variables>;
run;
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 338 views
  • 2 likes
  • 3 in conversation