如何比较宏中的日期值?

时间:2013-02-13 22:13:45

标签: macros sas sas-macro do-loops

这是我正在运行的宏....

%macro ControlLoop(ds);
            %global dset nvars nobs;
            %let dset=&ds;
            /* Open data set passed as the macro parameter */
            %let dsid = %sysfunc(open(&dset));
      /* If the data set exists, then check the number of obs ,,,then close the data set */
        %if &dsid %then %do;
              %If %sysfunc(attrn(&dsid,nobs))>0 %THEN %DO;;
                        %local dsid cols rctotal ;
                        %let dsid = %sysfunc(open(&DS));     
                        %let cols=%sysfunc(attrn(&dsid, nvars)); 
                    %do %while (%sysfunc(fetch(&dsid)) = 0);  /* outer loop across rows*/
                                        /*0:Success,>0:NoSuccess,<0:RowLocked,-1:eof reach*/

       %If fmt_start_dt<=&sysdate9 and fmt_end_dt>=sysdate9 %then %Do;
                            %do i = 1 %to &cols; 
        %local v t;   /*To get var names and types using 
                varname and vartype functions in next step*/
                                %let v=%sysfunc(varname(&dsid,&i)); /*gets var names*/
                                %let t = %sysfunc(vartype(&dsid, &i));  /*gets variable type*/
                                %let &v = %sysfunc(getvar&t(&dsid, &i));/*To get Var values Using 
                                                            GetvarC or GetvarN functions based on var data type*/
                            %end;
                            %CreateFormat(dsn=&dsn, Label=&Label, Start=&Start, fmtName=&fmtName, type=&type);
                        %END;
                        %Else %put ###*****Format Expired*****;
                    %END;
              %END;
              %else %put ###*****Data set &dset has 0 rows in it.*****;

              %let rc = %sysfunc(close(&dsid));
           %end;
         %else %put ###*****open for data set &dset failed - %sysfunc(sysmsg()).*****;
        %mend ControlLoop;

        %ControlLoop(format_control);

FOrmat_Control数据:

DSN :$12.  Label :$15. Start :$15. fmtName :$8. type :$3. fmt_Start_dt :mmddyy. fmt_End_dt :mmddyy.;
ssin.prd prd_nm prd_id mealnm 'n' 01/01/2013 12/31/9999
ssin.prd prd_id prd_nm mealid 'c' 01/01/2013 12/31/9999
ssin.fac fac_nm onesrc_fac_id fac1SRnm 'n' 01/01/2013 12/31/9999
ssin.fac fac_nm D3_fac_id facD3nm 'n' 01/01/2013 12/31/9999
ssin.fac onesrc_fac_id D3_fac_id facD31SR 'n' 01/01/2013 02/01/2012
oper.wrkgrp wrkgrp_nm wrkgrp_id grpnm 'n' 01/01/2013 12/31/9999

如何将fmt_Start_dt和fmt_end_dt与sysdate进行比较? 我在代码中尝试了类似%If fmt_start_dt<=&sysdate9 and fmt_end_dt>=sysdate9 %then %Do;的东西,但是值没有在循环中拾取....任何想法??? 提前谢谢....

1 个答案:

答案 0 :(得分:1)

我不完全确定你想要什么,但我认为这可行:

%if &fmt_start_dt <= %sysfunc(today()) and &fmt_end_dt >= %sysfunc(today()) 

您的FETCH函数会将数据集变量复制到宏变量,因此您需要使用&符号来引用它们。此外,您应该使用TODAY()函数而不是SYSDATE9宏变量。