【问题标题】:Using macro variable in an IF statement within a loop is not working在循环内的 IF 语句中使用宏变量不起作用
【发布时间】:2022-01-15 01:22:03
【问题描述】:

当我在 IF 语句中硬编码值(以 cmets 为单位)时,我的代码出现问题,但是当我插入宏变量时,“复制”和“删除”函数不起作用没有产生错误。下面是正在使用的代码:

*%let pathscr = //files/FEB_P000/Reporting_FS;

%let pathdes = //files/FEB_P000/Reporting_FS/Accounting log/2021;

%let fn = LFNPAccounting;

%let dt = %sysfunc(inputn(&acc_date, yymmddn8.),yymmddn8.); /* 20211209 */

%let Var = &fn&dt;/* LFNPAccounting20211209 */

data _null_;

  length fref $8 fname $256;

  did = filename(fref,'\\files\FEB_P000\Reporting_FS');

  did = dopen(fref);

  do i = 1 to dnum(did);

    fname = dread(did,i);

    newfn = SUBSTR(fname,1,22);

    if newfn = &Var then do;

    /*if newfn = 'LFNPAccounting20211209' then do;*/  

      rc1=filename('src',catx('/',"&pathscr",fname));

      rc2=filename('des',catx('/',"&pathdes",fname));

      rc3=fcopy('src','des');

      rc4= fdelete('src');

    end;

  end;

run;*

有人可以帮忙吗?

谢谢 汉斯

【问题讨论】:

    标签: sas


    【解决方案1】:

    我猜你尝试查看指定文件夹pathscr,如果文件与某个字符串(SUBSTR(fname,1,22))匹配,则将后者复制并删除到日志文件夹pathdes

    libname report "/home/kermit/temp/Reporting/";
    
    data report.have20211210
         report.have20211209
         report.have20211208;
        id = 1;
        output;
    run;
    
    %let pathscr = /home/kermit/temp/Reporting/;
    %let pathdes = /home/kermit/temp/Logs/;
    %let fn = have; /* Name of the file */
    %let type = .sas7bdat; /* File extension */
    %let dt = %sysfunc(inputn(%sysfunc(today()), yymmddn8.), yymmddn8.);
    
    %let file = &fn&dt&type.;
    %put &=file;
    
    data _null_;
       drop rc did;
       rc=filename("mydir", "&pathscr.");
       did=dopen("mydir");
       if did > 0 then do; /* check that the directory can be opened */
            do i=1 to dnum(did); /* use dnum() to determine the highest possible member number */
                fname=dread(did, i); /* get the name of the file */
                if fname = "&file." then do; /* if the name of the file match: */
                    rc=filename('src', "&pathscr&file.");
                    rc=filename('des', "&pathdes&file.");
                    rc=fcopy('src', 'des'); /* copy from source to destination */
                    rc=fdelete('src'); /* delete from source */
                end;
            end;
       end;
       else do; /* if directory cannot be open, put the error message to the logs */
          msg=sysmsg();
          put msg;
       end;
    run;
    
    Logs:
    FILE=have20211210.sas7bdat
    

    DOPEN 打开一个目录并返回一个目录标识符值(一个大于 0 的数字),用于在其他 SAS 外部文件访问函数中标识打开的目录。如果目录无法打开,DOPEN返回0,可以通过调用SYSMSG函数获取错误信息。

    为了方便起见,我将today() 用作dt 宏变量,但您必须将其更改为您要搜索的任何日期。

    考虑到上面的代码,如果文件已经在 Logs 文件夹中,它将不会被覆盖。请注意,如果您在指定路径的最后放置另一个 /,则不必使用 CATX 功能。

    结果

    【讨论】:

    • 感谢您的回复克米特!
    【解决方案2】:

    用单引号括起来的宏变量不会被解析。它们在双引号内时被解析。

    试试

    did = filename(fref,"&path_scr");
    

    【讨论】:

    • 感谢您的回答,理查德!
    【解决方案3】:

    您将 VAR 设置为如下值:

    %let Var = LFNPAccounting20211209 ;
    

    然后你用它来生成一个 SAS 语句:

    if newfn = &Var then do;
    

    这将解决

    if newfn = LFNPAccounting20211209 then do;
    

    由于我没有看到您创建任何名为 LFNPAccounting20211209 的变量,因此您很可能希望使用此语句:

    if newfn = "&Var" then do;
    

    这样您生成的 SAS 代码会将 NEWFN 的值与字符串文字而不是另一个变量进行比较。

    注意:由于您使用的是 WINDOWS 文件系统,因此您应该不区分大小写。

    if upcase(newfn) = %upcase("&Var") then do;
    

    【讨论】:

    • 太棒了,汤姆!它工作:)
    猜你喜欢
    • 1970-01-01
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多