我正在尝试更改原始数据文件的输出。在原始数据中,数据采用以下格式:
Name Test1 Test2 Test3 Test4
xyz 45 78 88 100
avb -1 89 76 29
但我想将数据结构更改为以下格式:
Name Score
xyz 45
xyz 78
xyz 88
xyz 100
xyz 89 (skip -1 because it's less than 0)
我正在尝试使用数组和输出语句,但遇到了麻烦。这是我的代码:
Data Stats;
set Record;
Array Test(*) Test1 - Test 6;
do i = 1 to 6;
if Test(i) gt -1 then output;
end;
run;
答案 0 :(得分:5)
您缺少的是将值移动到单个列中。现在你得到了正确的行数,但你没有获得单列。
Data Stats;
set Record;
Array Test(*) Test1 - Test 6;
do i = 1 to 6;
if Test(i) gt -1 then do;
score=test(i); *save value onto Score variable;
output;
end;
end;
keep score name; *only keep the two variables you want;
run;
答案 1 :(得分:2)
你可以使用proc transpose或带有do循环的Array语句来解决你的问题。
data record;
infile datalines missover;
input Name $ Test1 Test2 Test3 Test4;
datalines;
xyz 45 78 88 100
avb -1 89 76 29
;;;;
run;
/* Solution #1 : Using PROC TRANSPOSE */
proc transpose data=record out=solution_1(rename=(col1=score) where=(score>0) drop=_name_);
by notsorted name;
var test1--test4;
run;
proc print data=solution_1;run;
/* Solution # 2 : Using Array and do loop */
data solution_2;
set record;
array temp(*) test1-test4;
do i=1 to dim(temp);
score=temp[i];
if score >0 then output;
end;
keep name score;
run;
proc print data=solution_2;run;
答案 2 :(得分:1)
您可以使用非常简单的方法解决此问题。
data original;
input Name $ Test1-Test4;
cards;
xyz 45 78 88 100
avb -1 89 76 29
;
run;
***now you need to sort your data by Name *************;
proc sort data=original;
by Name;
run;
********************************************************;
data u_want;
set original;
array Test[4];
do i=1 to dim(Test);
if Test[i] >= 0 then Score=Test[i]; output;
end;
Drop i Test1-Test4;
run;
**********************************************************;
proc print data=u_want;
run;
Thanks!