BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Htuttle
Calcite | Level 5

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! 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
jklaverstijn
Rhodochrosite | Level 12

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

3 REPLIES 3
jklaverstijn
Rhodochrosite | Level 12

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.

Htuttle
Calcite | Level 5

Thanks Jan! It worked 🙂 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

sas-innovate-2024.png

 

Secure your spot at the must-attend AI and analytics event of 2024: SAS Innovate 2024! Get ready for a jam-packed agenda featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events.

 

Register by March 1 to snag the Early Bird rate of just $695! Don't miss out on this exclusive offer. 

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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