BookmarkSubscribeRSS Feed
sachinrathod14
Calcite | Level 5

If you have a string column with multiple date formats in it, the following code can be used to convert all the strings into a proper date format and store in a separate date column. The code is written in SAS Studio in SAS Viya 4.0.

 

 

Columns with multiple date format before running the code:

date01.png

Following code will read both the columns and apply the conditions to parse and convert date and create new date column with correct date format:

 
data find ;
    set j;
run;

data want;
  set j;
  PA_DOE_01=input(PA_DOE,??32.);
  if not missing(PA_DOE_01) then PA_DOE_01=int(PA_DOE_01)+'30dec1899'd;
  if missing(PA_DOE_01) then PA_DOE_01=input(prxchange('s/MONDAY|TUESDAY|SUNDAY//i',-1,PA_DOE),anydtdte40.);
  if missing(PA_DOE_01) then PA_DOE_01=input(tranwrd(PA_DOE,'00:00 ',' '),anydtdte40.);
 PA_DOI_01=input(PA_DOI,??32.);
  if not missing(PA_DOI_01) then PA_DOI_01=int(PA_DOI_01)+'30dec1899'd;
  if missing(PA_DOI_01) then PA_DOI_01=input(prxchange('s/MONDAY|TUESDAY|SUNDAY//i',-1,PA_DOI),anydtdte40.);
  if missing(PA_DOI_01) then PA_DOI_01=input(tranwrd(PA_DOI,'00:00 ',' '),anydtdte40.);
  format PA_DOE_01 PA_DOI_01 date9.;
run;
 

After running the above code, the two output columns PA_DOI_01 and PA_DOE_01 are the date columns with correct date format.

 date02.png

2 REPLIES 2
ballardw
Super User

You might also explain where the data set J actually comes from. 

 

There are hints that this involves Excel as a possible data source. Which may mean that manipulation of the data set in Excel prior to reading into a SAS data set might be a better solution such as setting a single date format in the Excel column. Or save to a CSV text file and use a data step to read the data.

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 384 views
  • 2 likes
  • 3 in conversation