Hi all:
If I have a data like 2014-06-09, please advise me how to input it from a text file into date format as in SAS file.
Thanks.
data have;
input char_date : $20. fmt : $20.;
num_char=inputn(char_date,fmt);
format num_char date9.;
cards;
12-08-2015 DDMMYY12.
12/8/2016 MMDDYY12.
05/25/2015 MMDDYY12.
;
run;
proc print;run;
do you mean
input date yymmdd10.;
format date yymmdd10.;
Using the Informat YYMMDD10. ought to work.
What about 12/9/2018?
do you have a mix of date values with some being yymmdd ,yymmdd and ddmmyy?
@ybz12003 wrote:
What about 12/9/2018?
That is actually ambiguous as we cannot tell from the given value which is the month and which is the day.
There are a whole slew of date informats
mmddyy
ddmmyy
yymmdd
date
If your data in provided external file has varied date formats there is even ANYDTDTE. but that guesses about some sorts of values and they may not match your need.
@ybz12003 wrote:
What about 12/9/2018?
Do a google search for "sas date informats", and then select an informat from the documentation that suits your input data.
I don't have a mix date format, but I do have the different date format due to the source coming from the different places. For example,
A city's date format is 12-08-2015
B city's date format is 12/8/2016
C city's date format is 05/25/2015
Does your source data looks like
A city's date format is 12-08-2015
B city's date format is 12/8/2016
C city's date format is 05/25/2015
If yes, let us know how you want to read it? Should i assume
A city's date format is --one char variable
12-08-2015--date variable
?
would be clear to let know what you want
A city's date format is 12-08-2015
B city's date format is 12/8/2016
C city's date format is 05/25/2015
As pointed out above, this is ambiguous. Can you clarify if A and B are month-day-year, or day-month-year?
Also, the different delimiters are not really a problem here, the format should read the dates properly even if the delimiters change.
A city's date format is 12-08-2015 DD-MM-YYYY
B city's date format is 12/8/2016 MM/D/YYYY
C city's date format is 05/25/2015 MM/DD/YYYY
After you have identified in your own mind which city uses which convention as far as month-day-year or day-month-year, you need to write code so that the city will be read via the proper informat, which is MMDDYY. or DDMMYY. or possibly others
Maybe something like this
data want;
input chardate $10. othervariables ... ;
if city='A' then date=input(chardate,ddmmyy10.);
else if city in ("B","C") then date=input(chardate,mmddyy10.);
format date date7.;
run;
data have;
input char_date : $20. fmt : $20.;
num_char=inputn(char_date,fmt);
format num_char date9.;
cards;
12-08-2015 DDMMYY12.
12/8/2016 MMDDYY12.
05/25/2015 MMDDYY12.
;
run;
proc print;run;
Thanks for all of your great suggestion. It helps! 🙂
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.