BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
paddyb
Quartz | Level 8

data demo;
length dt $9;
dt='mar2010';output;
dt='2007';output;
dt='1feb2010';output;
dt='';output;
dt='32010';output;
dt='142010';output;
run;

i have to separate month,date,year from above dataset,i tried

year= SUBSTR(TRIM(dt),LENGTH(TRIM(dt))-3);
month=SUBSTR(TRIM(dt),LENGTH(TRIM(dt))-6,3);

to get year and month values but i m not getting date value with this logic.

any other way to separate it?

thanks

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21
data demo;
length dt $9;
dt='mar2010';output;
dt='2007';output;
dt='1feb2010';output;
dt='';output;
dt='32010';output;
dt='142010';output;
run;
data want;
  set demo;
  format date date9.;
  if length(dt) eq 4 then date=mdy(1,1,dt);
  else if length(dt) eq 5 then date=mdy(1,first(dt),substr(dt,2));
  else if length(dt) eq 6 then date=
    mdy(1,substr(dt,1,2),substr(dt,3));
  else date=input(dt,anydtdte9.);
  month=month(date);
  day=day(date);
  year=year(date);
  if length(dt) eq 7 then call missing(day);
  else if length(dt) in (5,6) then call missing(month);
  else if length(dt) eq 4 then do;
    call missing(month);
    call missing(day);
  end;
run;

Art, CEO, AnalystFinder.com

 

View solution in original post

4 REPLIES 4
art297
Opal | Level 21

What month, day and year to you expect to get from each of the following:

mar2010
2007
32010
142010

 

Art, CEO, AnalystFinder.com

 

paddyb
Quartz | Level 8

for 32010 day=3 month= missing and yr =2010

for mar2010 day=missing month=march yr=2010

for 2007 day=missing month=missing yr=2007

for142010 day=14 month=missing yr=2010

art297
Opal | Level 21
data demo;
length dt $9;
dt='mar2010';output;
dt='2007';output;
dt='1feb2010';output;
dt='';output;
dt='32010';output;
dt='142010';output;
run;
data want;
  set demo;
  format date date9.;
  if length(dt) eq 4 then date=mdy(1,1,dt);
  else if length(dt) eq 5 then date=mdy(1,first(dt),substr(dt,2));
  else if length(dt) eq 6 then date=
    mdy(1,substr(dt,1,2),substr(dt,3));
  else date=input(dt,anydtdte9.);
  month=month(date);
  day=day(date);
  year=year(date);
  if length(dt) eq 7 then call missing(day);
  else if length(dt) in (5,6) then call missing(month);
  else if length(dt) eq 4 then do;
    call missing(month);
    call missing(day);
  end;
run;

Art, CEO, AnalystFinder.com

 

paddyb
Quartz | Level 8

Thank you sir

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!

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.

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
  • 4 replies
  • 832 views
  • 1 like
  • 2 in conversation