BookmarkSubscribeRSS Feed
gopilth
Calcite | Level 5

Hi,

I am trying to convert partial dates using below code but unable to get any output. Can you please review and advise what is missing. Thank you.

required output -- data d2

DAT_Rdt_new
19 UNK 201219JAN2012
UN UNK 201201JAN2012
22 OCT 201222OCT2012
06 NOV 201206NOV2012
UN UNK 201301JAN2013
UN MAY 2013
01MAY2013

Data d2 (keep=  dt_new DAT_R );

Set d1 ;

if scan(DAT_R,2) = 'UNK' and scan(DAT_R,1) = 'UN' then do;

  day = '01';

  month = 'JAN';

  dt_new = input(compbl(day) || compbl(month) || scan(DAT_R,3),date9.);

  end;

  else if substr(DAT_R,1,2) in ('UN','un') then do;

  day = '01';

  dt_new = input(compbl(day) || substr(compress(DAT_R),3,7),date9.);

  end;

  else do;

  dt_new = input(compress(DAT_R),date9.);

  end;

  format dt_new date9.;

Run;

3 REPLIES 3
ballardw
Super User

Instead of building a complicated string try using the MDY function

day = input(scan(dat_r,1),best4.); /* which will be missing if not a numeric value*/

month = input (scan(dat_r,2),best4.);

year = input (scan(dat_r,3),best4.);

if day=. then day=1;

if month=. then month=1;

dt_new=mdy(month, day,year);

Reeza
Super User

Use tranwrd function to replace UNK with Jan and UN with 01.

Use compress and input function to get dates.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

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
  • 5917 views
  • 0 likes
  • 4 in conversation