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

data date;
input sdate $10.;
datalines;
1990
5 12 2001
10 10 2010
8 2018
1990
20 1 2017
12 2016

5 2017
15 12 2017
;
run;

I want 2 variables which are in bold with the below conditons

New date= variable

if day is missing then set to 15
if day and month missing then set to january 01
if day,month & year are missing then set to today's date

Abbreviation = variable

 if day is missing then set to D
if day and month missing then set to M
if day,month & year are missing then set to Y
Create brthdtc and dtcflag variables by using day, month & year variables.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Count the number of "words" and then adjust from there.

data want;
  set have;
  dtcflag = char('YMD ',countw(sdate)+1);
  select (dtcflag);
    when (' ') brthdtc = input(sdate,ddmmyy10.);
    when ('D') brthdtc = input('15 '||sdate,ddmmyy10.);
    when ('M') brthdtc = input('01 01 '||sdate,ddmmyy10.);
    otherwise brthdtc=date();
  end;
  format brthdtc date9.;
run;
Obs    sdate         dtcflag      brthdtc

  1    1990             M       01JAN1990
  2    5 12 2001                05DEC2001
  3    10 10 2010               10OCT2010
  4    8 2018           D       15AUG2018
  5    1990             M       01JAN1990
  6    20 1 2017                20JAN2017
  7    12 2016          D       15DEC2016
  8                     Y       28JUL2020
  9    5 2017           D       15MAY2017
 10    15 12 2017               15DEC2017

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

Count the number of "words" and then adjust from there.

data want;
  set have;
  dtcflag = char('YMD ',countw(sdate)+1);
  select (dtcflag);
    when (' ') brthdtc = input(sdate,ddmmyy10.);
    when ('D') brthdtc = input('15 '||sdate,ddmmyy10.);
    when ('M') brthdtc = input('01 01 '||sdate,ddmmyy10.);
    otherwise brthdtc=date();
  end;
  format brthdtc date9.;
run;
Obs    sdate         dtcflag      brthdtc

  1    1990             M       01JAN1990
  2    5 12 2001                05DEC2001
  3    10 10 2010               10OCT2010
  4    8 2018           D       15AUG2018
  5    1990             M       01JAN1990
  6    20 1 2017                20JAN2017
  7    12 2016          D       15DEC2016
  8                     Y       28JUL2020
  9    5 2017           D       15MAY2017
 10    15 12 2017               15DEC2017
honeyblue
Calcite | Level 5

Can you please explain in details step by step

 

Thank You

Tom
Super User Tom
Super User
data want;
  set have;
  dtcflag = char('YMD ',countw(sdate)+1);
  select (dtcflag);
    when (' ') brthdtc = input(sdate,ddmmyy10.);
    when ('D') brthdtc = input('15 '||sdate,ddmmyy10.);
    when ('M') brthdtc = input('01 01 '||sdate,ddmmyy10.);
    otherwise brthdtc=date();
  end;
  format brthdtc date9.;
run;

The COUNTW() function counts the number of words.  So you should get 0,1,2 or 3 words from your string.  The CHAR() function returns the character in the position specified.  Positions count from 1 so you need to add one to the number of words in case there are no words in the string.  So when there are no words you take the first character, Y.

The SELECT statement is a multi way branching tools. So based on whether the flag is one of the four possible value a different statement is called.

When you have no words use today's date.

When you have one word prefix the string with 01 01 and convert it to a date.

When you have two words prefix the string with 15 and convert it to a data.

When you have three words just convert the string as it is to a date.

The DDMMYY informat reads strings in DMY order and converts them into date values.

The FORMAT statement attaches the DATE format to the variable so the number of days stored in it will print so that humans can understand it.

 

 

 

 

honeyblue
Calcite | Level 5
thank you

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 970 views
  • 0 likes
  • 2 in conversation