简体   繁体   English

SAS通过列循环

[英]SAS Loop through Columns

I am trying to write code in SAS which loops through the columns. 我正在尝试在SAS中编写遍历各列的代码。

DesiredVariable is what I am trying to calculate using a Loop DesiredVariable是我要使用循环计算的内容

where if the LengthValue for a Row is 5 Then the DesiredVariable value = Length5 and if the LengthValue for a Row is 3 Then the DesiredVariable value = Length3 如果行的LengthValue为5,则DesiredVariable值= Length5;如果行的LengthValue为3,则DesiredVariable值= Length3

(Image attached) (附图片)

I would really appreciate any help writing this loop because I am relatively new to SAS. 我真的很感谢编写此循环的任何帮助,因为我对SAS比较陌生。

Thanks in advance. 提前致谢。

在此处输入图片说明

You don't need to loop the rows, you only need to create an Array for the columns length1-length5 and use LengthValue as the pointer. 您不需要循环行,只需要为length1-length5列创建一个Array ,并使用LengthValue作为指针。

data have;
input Name $ LengthValue Length1 Length2 Length3 Length4 Length5 DesiredVariable;
datalines;
Sally 5 0.29 0.21 0.73 0.46 0.43 .
Andrea 2 0.97 0.76 0.5 0.33 0.41 .
;
run;
data want;
set have;
Array length[*] Length1-Length5;
DesiredVariable=length[LengthValue];
run;

Output: 输出:

Name=Sally LengthValue=5 Length1=0.29 Length2=0.21 Length3=0.73 Length4=0.46 Length5=0.43 DesiredVariable=0.43
Name=Andrea LengthValue=2 Length1=0.97 Length2=0.76 Length3=0.5 Length4=0.33 Length5=0.41 DesiredVariable=0.76

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

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