Reputation: 1246
I am trying to change the output of my raw data file. In the raw data, the data are in the following format:
Name Test1 Test2 Test3 Test4
xyz 45 78 88 100
avb -1 89 76 29
But I want to change the data structure to the following format:
Name Score
xyz 45
xyz 78
xyz 88
xyz 100
xyz 89 (skip -1 because it's less than 0)
I am trying to use array and output statement but having trouble. Here is my code:
Data Stats;
set Record;
Array Test(*) Test1 - Test 6;
do i = 1 to 6;
if Test(i) gt -1 then output;
end;
run;
Upvotes: 3
Views: 158
Reputation: 11
You can solve this problem with the very easy method.
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!
Upvotes: 1
Reputation: 1576
You can use proc transpose or Array statement with do loop to solve your problem.
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;
Upvotes: 2
Reputation: 63424
What you're missing is moving the value into a single column. Right now you're getting the right number of rows, but you're not getting the single column.
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;
Upvotes: 5