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.);
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.