data date;
input date n $ 14. ;
cards;
01apr2022
01/05/2022
2022/02/28
2022/02/20
15-08-2020
;
I need output like following
n
2022/04/01
2022/05/01
28feb2022
20feb2022
2020/08/15
Try ANYDTDTE informat while importing the data.
But then I don't understand the requirement o to have different date formats in your output?
As you have stated the problem, with the output dates in different formats, this is a particularly unpleasant and difficult problem to solve, and probably has to be done via character string manipulation within a DATA step. I choose not to write such code, and rather advise you to accept output that has the same format on every row. This makes the solution much easier.
If you are willing to accept this modification to the problem, then you still have all of your inputs are in different formats, this is much easier to solve by using the informat ANYDTDTE10. Still, it would be better to manage the process better and have all input dates in the same format.
data date;
input date :anydtdte10.;
format date date9.;
cards;
01apr2022
01/05/2022
2022/02/28
2022/02/20
15-08-2020
;
This works, but if the mixture of input dates includes
01/05/2022 (to mean january 5, 2022)
and
05/01/2022 (to also mean january 5, 2022)
then you have additional complications that I leave you to deal with.
Without a very clear reason, as in terms of someone paying me real money, I would never create "date" values with different formats.
Please explain in some detail the actual advantage derived from turning " 01apr2022" into "2022/04/01" while at the same time requiring "2022/02/20" to become "20feb2022". Really what use is this???
Why do you have that N on your Input statement? There is only one variable. And your code as written will create a lot of missing values. As written you are attempting to read the variable Date as simple numeric variable and none of the values are valid as such.
IF you create SAS date values instead of random strings then you can apply a format as desired at time of use such as:
data date; input date anydtdte15. ; file print; put "Date9 format" date= date9.; put "yymmdd format" date= yymmdd10.; put "ddmmyy format" date= ddmmyy10.; put "mmddyy format" date= mmddyy10.; cards; 01apr2022 01/05/2022 2022/02/28 2022/02/20 15-08-2020 ;
The informat Anydtdte will attempt to read values as dates from different text layouts the potentially resemble dates.
Once you have a SAS date value then you can apply formats as desired for many purposes but generally to the VARIABLE, not per observation. SAS provides many date display formats plus with proc format you can even create your own.
You would have to provide a lot or description as to why a specific original layout gets displayed into a different one later and no such logic was provided.
@112211 wrote:
data date;
input date n $ 14. ;
cards;
01apr2022
01/05/2022
2022/02/28
2022/02/20
15-08-2020
;
I need output like following
n
2022/04/01
2022/05/01
28feb2022
20feb2022
2020/08/15
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 16. Read more here about why you should contribute and what is in it for you!