I am new for SAS. Now, I am learning to read and merge the data sets together.
I have a csv dataset to input and I use the "infile" in my data step
In this file the "date" Variable have 2 data format: date7 and ddmmyy10
What method shall I use to input it?
You will have to read in the date variable as a character variable and then convert it to a SAS date. Use a function that will show you what format the character date is in so you use the appropriate informat when converting it to a SAS date. I chose the compress and length functions:
data test;
input chardate $10.;
if length(compress(chardate,,'a')) ne length(chardate) then date=input(chardate,date7.);
else date=input(chardate,ddmmyy10.);
format date mmddyy10.;
cards;
01sep15
01092015
;;;;
run;
SAS has an informat ANYDTDTE that will read many different standard date formats.
If you're writing the code it would be a good idea to specify the display format that you prefer.
informat thisdate anydtdte.;
format thisdate mmddyy10.;
as one example.
I dont think that informat will work here:
data test;
input date anydtdte.;
format date mmddyy10.;
cards;
01sep15
01092015
;;;;
run;
test shows two different dates.
Behavior depends on DATESTYLE option;
options datestyle=DMY;
data test;
input date anydtdte.;
format date mmddyy10.;
cards;
01sep15
01092015
09012015
;;;;
run;
options datestyle=MDY;
data test;
input date anydtdte.;
format date mmddyy10.;
cards;
01sep15
01092015
09012015
;;;;
run;
Important information for using that informat. Thanks.
You will have to read in the date variable as a character variable and then convert it to a SAS date. Use a function that will show you what format the character date is in so you use the appropriate informat when converting it to a SAS date. I chose the compress and length functions:
data test;
input chardate $10.;
if length(compress(chardate,,'a')) ne length(chardate) then date=input(chardate,date7.);
else date=input(chardate,ddmmyy10.);
format date mmddyy10.;
cards;
01sep15
01092015
;;;;
run;
Thanks a lot for answering
I have used the ANYDTDTE informate but it seems it only work on your code but not mine.
At last I used the method of reading in the date variable as a character variable and converting it into a SAS date.
I modified a little that I used :
length chardat $ 10 default=7;
infile"the csv file";
input chardate;
if length(chardate) ne 10 then date=input(chardate,date7.);
else date=input(chardate,ddmmyy10.);
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.