BookmarkSubscribeRSS Feed
112211
Obsidian | Level 7

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

 
 
3 REPLIES 3
LinusH
Tourmaline | Level 20

Try ANYDTDTE informat while importing the data.

But then I don't understand the requirement o to have different date formats in your output?

Data never sleeps
PaigeMiller
Diamond | Level 26

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.

 

 

--
Paige Miller
ballardw
Super User

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

 
 

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 441 views
  • 1 like
  • 4 in conversation