保留由宏创建的变量

时间:2019-07-29 20:07:58

标签: sas

我有以下代码重命名列名;我只想保留由宏创建的变量。我确实意识到我可以删除旧变量,但是很好奇是否可以在宏中放置一个keep选项。

因此,例如,在数据步骤中,我只想保留以'%transform_this(JUNE19)'开头的变量;

谢谢!

 %macro transform_this(x);
 &x._Actual=input(Current_Month, 9.0);
 &x._Actual_Per_Unit = input(B, 9.);
 &x._Budget=input(C, 9.);
 &x._Budget_Per_Unit=input(D, 9.);
 &x._Variance=input(E, 9.);
 &x._Prior_Year_Act=input(G, 9.);
 Account_Number=input(H, 9.);
 Account_Description=put(I, 35.);
 &x._YTD_Actual=input(Year_to_Date, 9.);
 &x._YTD_Actual_Per_Unit=input(L, 9.);
 %mend transform_this;

 data June_53410_v1;
 set June_53410;
 %transform_this(JUNE19);
 if Account_Description='Account Description' then DELETE; 
 Drop Current_Month B C D E G H I Year_to_Date L M N;
 run; 

3 个答案:

答案 0 :(得分:3)

keep June19_: Account_:;

这将保留所有从June19_和Account_开始的变量,这显然是您需要的变量。

答案 1 :(得分:2)

  

我很好奇是否可以在宏内放置一个保留选项。

您绝对可以在宏中使用keep

%macro transform_this(x);
    keep &x._Actual &x._Actual_Per_Unit
         &x._Budget &x._Budget_Per_Unit
         &x._Variance &x._Prior_Year_Act
        Account_Number Account_Description 
        &x._YTD_Actual &x._YTD_Actual_Per_Unit
    ;

    &x._Actual=input(Current_Month, 9.0);
    /* ...and the rest of your code */
%mend transform_this;

您是否以为不能这么做?

答案 2 :(得分:2)

在数据步骤中添加两个哨兵变量,一个在宏调用之前,另一个在宏调用之后。在--语句中使用双破折号keep变量名称列表构造,并将标记放置在数据步骤输出数据集说明符drop=选项中。

data want (drop=sentinel1 sentinel2); /* remove sentinels */
  set have;
  retain sentinel1 0;
  %myMacro (…)
  retain sentinel2 0;
  … 
  keep sentinel1--sentinel2;  * keep all variables created by code between sentinel declarations;
run;
  

Name Range Lists

     

名称范围列表取决于变量定义的顺序,如下所示   下表:

     

名称范围列表

     

Variable List包含的变量
  x -- a所有变量按变量定义的顺序,从   变量x将变量a包括在内
  x -NUMERIC- a所有从变量x到变量a的数字变量
  x -CHARACTER- a从变量x到变量a的所有字符变量

     

注意:请注意,名称范围列表使用双连字符(--)来指定   变量之间的范围和编号范围列表使用单个   连字符指定范围。