【问题标题】:sas macro code to check missing dates用于检查缺失日期的 sas 宏代码
【发布时间】:2016-02-04 08:03:38
【问题描述】:

如果输入日期在下面给出,那么日期输出应该是给定的

我希望 sas 宏代码检查输入中缺少的日期

    1.-MMM-YYYY = 01-MMM-YYYY
    --YYYY    = 01-JAN-YYYY 
    DD--YYYY  = DD-JAN-YYYY 
    DD-MMM-   = DD-MMM-0000

【问题讨论】:

  • 我该如何逻辑这个问题?需要帮助...
  • 以上输入缺少日、月,我可以先读取str然后查看wats缺失然后显示相应的输出
  • 一旦您确定日期不完整,扫描每个部分并根据业务规则填写缺失的位并重新组合。
  • Peeyush,您过去曾问过问题,但没有确认您是否得到答案。除了提问之外,请花时间识别所提供的答案,以便人们继续回答您的问题。

标签: sas sas-macro


【解决方案1】:

我创建了一个数据集,其中包含缺少日、月和年的所有可能情况。下面的数据步骤解决了所有可能性以及如何处理它们以按照您请求的格式创建结果变量 new_bdate。

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;

【讨论】:

    【解决方案2】:

    Peeyush, 如果您在问题中输入的列表是完整的,那么下面的代码段是获得您想要的结果的简单方法。我放了一个宏和非宏的 if-then 语句来处理它。

     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;
    

    【讨论】:

    • 但我的输入数据集有以下值--> 数据有;输入生日$;牌; -Jan-1975 --1977 02--1978 03-Jan-run;
    • 我的输入如上面给出的评论有 4 个 obs,特定 obs 缺少日、月和年,& 在 obs 2 中缺少日和月 &3 obs 缺少月份。那么我们如何使用 if else 条件来显示日期输出。
    • 只需将你的数据放入宏 %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';跑; %修正检查未命中; %checkmiss;
    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多