【问题标题】:Rename variables in SAS using macros使用宏重命名 SAS 中的变量
【发布时间】:2021-09-08 06:18:26
【问题描述】:

我有一个数据集,其中包含过去 12 个月的借方和贷方营业额以及客户的平均余额。数据看起来像这样

|帐号 | Monthly_credit_turover1 || Monthly_credit_turover2 |average_bal_1| average_bal_2

我想用过去 12 个月替换 1,2,3 到 12。例如,average_bal_1 应对应于 average_bal_march,average_bal_2 应替换为 average_bal_april。所以对于所有带 1 的变量应该对应于三月,2 对应于四月,依此类推

我写的代码是

%macro renaming(i=, mon=);



data bands_macro;
set may_1;
 *this just renames one variable with the two input parameters;
turnover_&i.=sum(MonthlyCreditTurnover&i., MonthlyDebitTurnover&i.);
format tn_bands $50.;
if turnover_&i. le 0 then tn_bands="1. LE 0";
else if turnover_&i. gt 0 and turnover_&i. le 1000  then tn_bands="2. 0-1k";
else if turnover_&i. gt 1000 and turnover_&i. le 4000  then tn_bands="3. 1k-4k";
else if turnover_&i. gt 4000 and turnover_&i. le 10000  then tn_bands="4. 4k-10k";
else tn_bands="5. >10k";
format ab_bands $50.;
if averagebalance&i. =999999999999 or averagebalance&i. le 0 then ab_bands="1.LE 0";
else if averagebalance&i. gt 0 and averagebalance&i. le 1000  then ab_bands="2. 0-1k";
else if averagebalance&i. gt 1000 and averagebalance&i. le 5000  then ab_bands="3. 1k-5k";
else if averagebalance&i. gt 5000 and averagebalance&i. le 10000  then ab_bands="4. 5k-10k";
else if averagebalance&i. gt 10000 and averagebalance&i. le 25000  then ab_bands="5. 10k-25k";
else if averagebalance&i. gt 25000 and averagebalance&i. le 50000  then ab_bands="6. 25k-50k";
else ab_bands="7. >50k";
drop MonthlyCreditTurnover&i. MonthlyDebitTurnover&i.;
run;
%mend;
%renaming(i=1,mon=Mar21);
%renaming(i=2,mon=Feb21);

但不幸的是,我在运行此代码时收到此警告-

不能将变量营业额 2 重命名为 turover_april,因为营业额_april 已经存在。我如何在单个数据集中进行这些更改

【问题讨论】:

  • 打开 MPRINT 选项以查看宏生成的代码为何会导致该错误。
  • 你怎么知道1应该重命名为March呢?为什么不是一月?还是八月?

标签: loops sas sas-macro


【解决方案1】:

所以,这是一个简单的数据内步骤宏的很好使用。

data mydata;
  turnover1 = 1;
  turnover2 = 2;
run;

*give two parameters, the numeric value and the month to convert to;
%macro renaming(i=, mon=);
*this just renames one variable with the two input parameters;
rename turnover&i.= turnover_&mon.;
%mend;

data want;
  set mydata;
  *now call each of the macro iterations separately;
  %renaming(i=1,mon=mar)
  %renaming(i=2,mon=apr)
  ;   *just to make the highlighting work;
run;

当然,可以提出一个更好的列表:

*dataset of the i=mon relationships, you can have this in excel or whatever;
 *and change it every month when the other stuff updates;
*you also may be able to "calculate" all of this?;
data renames;
  input i mon $;  
  datalines;
1 mar
2 apr
;;;;
run;

*pull that into a macro variable using PROC SQL SELECT INTO;
proc sql;
  select cats('%renaming(i=',i,',mon=',mon,')')
    into :renamelist separated by ' '
    from renames;
quit;


*now apply the rename list;
data want;
  set mydata;
  &renamelist.
  ;
run;

【讨论】:

  • @Joe-当我编写此代码并在两个月内运行时-当它第二次运行循环时,它仅适用于最后一个变量而不是所有变量,我在循环。
  • @joe- 我已经编辑了代码-为了向您展示最终数据集中发生的事情-它只是在 feb 循环上工作,而不是显示以前的结果
  • 不知道你为什么要评论我的回答;我建议您以非常不同的方式执行此操作,听起来您并没有这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-30
  • 1970-01-01
  • 2017-03-08
  • 2019-12-01
  • 2017-10-04
相关资源
最近更新 更多