简体   繁体   English

SAS:来自数据集的最高变量的输出

[英]SAS : Output from highest variable from dataset

I want to assign a new variable from existing highest n variable. 我想从现有的最高n变量中分配一个新变量。 So if we have a table that has increasing number of columns - 因此,如果我们的表的列数不断增加-

data have;
input uid $ var1 $ var2 $ var3 $;
datalines;                      
1111    1   0   1
2222    1   0   0
3333    0   0   0
4444    1   1   1
5555    0   0   0
6666    1   1   1
;

I want derive the variable var3 as final_code. 我想将变量var3导出为final_code。

data want;
set have;
final_code = max(of var1-var3);
run;

Above doesn't make sense here as I want only var3 column to remain. 上面没有意义,因为我只想保留var3列。 Similarly, if var4 is there, I wish to have var4 only. 同样,如果存在var4,我希望仅拥有var4。

Does somebody want to help me here ? 有人想在这里帮助我吗?

If I understand you right, you don't want max of the values but the value from the highest-numbered-variable. 如果我理解正确,那么您就不需要max值,而是最大编号变量中的值。

Lots of ways to do this, which way depends on how the variables are named. 有很多方法可以做到这一点,这取决于变量的命名方式。 Here's the easiest, if they're actually named as you say. 如果按照您所说的名称实际命名,这是最简单的。

data want;
  set have;
  array var[*] var:;
  final_code = var[dim(var)];
run;

Here we make an array out of var: and then choose the last element in the array using dim (to say the size of the array). 在这里,我们用var:组成一个数组,然后使用dim (表示数组的大小)选择数组中的最后一个元素。

I think this is what you are looking for is: 我认为这是您正在寻找的是:

%let n=3
data want;
set have;
var&n = max(of var1-var&n);
drop var1-var%eval(&n-1);
run;

The macro variable &n holds the value of n. 宏变量&n保留&n的值。 This acts as a substitution during the compilation phase of the code. 在代码的编译阶段,这可以替代。

The DROP statement tells the data step to drop those variable. DROP语句告诉数据步骤删除那些变量。

The %eval() macro function performs integer math on macro values. %eval()宏函数对宏值执行整数数学运算。 So we are dropping 1 through N-1. 因此,我们将N-1丢弃1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM