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!
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.
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.
Thanks Jan! It worked 🙂
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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.