-3

If the input date is given below then the date output should be as given

I want sas macro code to check missing date in the inputs

    1.-MMM-YYYY = 01-MMM-YYYY
    --YYYY    = 01-JAN-YYYY 
    DD--YYYY  = DD-JAN-YYYY 
    DD-MMM-   = DD-MMM-0000
Apache11
  • 189
  • 11
  • how can i logic this question? need some help... – Apache11 Feb 04 '16 at 08:06
  • the above inputs have missing day, month, can I first read the str then see wats missing then display the respective outputs – Apache11 Feb 04 '16 at 09:06
  • Once you determine that a date is incomplete SCAN each part and fill in the missing bits depending on business rule and recombine. – data _null_ Feb 04 '16 at 11:14
  • Peeyush, you have asked questions in the past without confirming if you got your answer. Please take the time to recognize answers provided in addition to asking questions so folks will continue to answer your questions. – Keneni Feb 04 '16 at 14:35

2 Answers2

0

Peeyush, If the list you put in the question is complete, the code segment below is a simple way of getting the results you want. I put both a macro and non-macro if-then statement to handle it.

 data inputss;
       input in  $  ;
       cards;
    -MMM-YYYY 
    --YYYY    
    DD--YYYY  
    DD-MMM-   
    ;

data result;
   set inputs;
if in = '-MMM-YYY' then Date_Output = '01-MMM-YYYY';
else if in = '--YYYY' then Date_Output  = '01-JAN-YYYY' ;
else if in = 'DD--YYYY' then Date_Output  = 'DD-JAN-YYYY' ;
else if in = 'DD-MMM-' then Date_Output   = 'DD-MMM-0000';
run;



%macro checkmiss;
    %if  in = '-MMM-YYY' %THEN Date_Output = '01-MMM-YYYY';
    %else %if  in = '--YYYY' %THEN Date_Output  = '01-JAN-YYYY' ;
    %else %if  in = 'DD--YYYY' %THEN Date_Output  = 'DD-JAN-YYYY' ;
    %else %if  in = 'DD-MMM-' %THEN Date_Output   = 'DD-MMM-0000';
run;
%mend checkmiss;
%checkmiss;

enter image description here

Keneni
  • 705
  • 5
  • 12
  • but my input dataset has following values--> data have; input birthdate $; cards; -Jan-1975 --1977 02--1978 03-Jan- run; – Apache11 Feb 05 '16 at 21:11
  • my inputs are as given above comment there are 4 obs, particular obs has missing day,month &year, & in obs 2 there is missing day & month &3 obs has missing month. so how can we use if else condition to display the date output. – Apache11 Feb 05 '16 at 21:38
  • Just put your data into the macro %macro checkmiss; %if in = '-Jan-1975' %THEN Date_Output = '01-Jan-1975'; %else %if in = '--1977' %THEN Date_Output = '01-JAN-1977' ; %else %if in = 02--1978' %THEN Date_Output = '02-JAN-1978' ; %else %if in = 03-Jan-' %THEN Date_Output = '03-Jan-0000'; run; %mend checkmiss; %checkmiss; – Keneni Feb 08 '16 at 19:50
0

I created a dataset with all possible situations of missing day, month and year. The datasteps below address all possibilities and how to handle them to create the result variable new_bdate in the format you requested.

data have; 
  input birthdate $20.; 
   datalines4;
  -JAN-1975 
  --1977 
  02--1978 
  03-JAN- 
  01-JAN-1980
  03--
  -JAN-
;;;;
run;

data haha2(Keep = birthdate new_bdate);
  set have;  
   format day $2. month $3. Year $4. New_bdate $11.;
   call scan(birthdate, 1, First_Pos, First_Length);
   First_Word = substrn(birthdate, First_Pos, First_Length);
   call scan(birthdate, -1, Last_Pos, Last_Length);
   Last_Word = substrn(birthdate, Last_Pos, Last_Length);
   if length(birthdate) = 11 then length1 = 'Y';
  else length1 = 'N';

   if length1 = 'Y' then do;
      Day = substr(birthdate,1,2);
      Month = substr(birthdate,4,3);
      Year = substr(birthdate,8,4);
   end;
   else if length1 = 'N' then do;
     if (First_Pos = '1' and Last_Length = '2')  then do 
              Day = First_Word;
              Month = 'JAN';
              Year = '0000';
        end;
      else if (First_Pos = '1' and Last_Length = '3')  then do 
              Day = First_Word;
              Month = Last_Word;
              Year = '0000';
        end;
     else if (First_Pos = '1' and Last_Length = '4')  then do 
              Day = First_Word;
              Month = 'JAN';
              Year = Last_Word;
        end;
   if (First_Pos = '2' and Last_Length = '3')  then do 
              Day = '01';
              Month = First_Word;
              Year = '0000';
        end;
      else if (First_Pos = '2' and Last_Length = '4')  then do 
              Day = '01';
              Month = First_Word;
              Year = Last_Word;
        end;
     else if (First_Pos = '3' and Last_Length = '4')  then do 
              Day = '01';
              Month = 'JAN';
              Year = Last_Word;
        end;
    end;
New_bdate = Day||'-'||Month||'-'||Year;
run;

enter image description here

Keneni
  • 705
  • 5
  • 12