I have a data set that looks like this:
data SurgerySatisfaction; input ID$ Knee_1or2$ Score_PreOp Score_1Day Score_1Week Score_1Month; cards; 01 1 0 5 7 10 02 1 0 10 15 15 02 2 3 5 8 10 03 1 0 3 3 3 03 2 0 6 9 9 04 1 0 4 10 10 ; run;
Basically, the data set records patient ID, which knee got replacement surgery (1 or 2), and satisfaction scores at a certain time before/after the surgery.
The task then asked to use the array staatement to create a new data set that contains satisfaction scores for knee 1, and the output is supposed to look like this:
id visit score 01 1 0 01 2 5 01 3 7 01 4 10 02 1 0 02 2 10 02 3 15 02 4 15 03 1 0 03 2 3 03 3 3 03 4 3 04 1 0 04 2 4 04 3 10 04 4 10
How is this acheived by the array statement? I guess I also need to better understand what the whole point of array statements is.
Given this is an exercise we shouldn't just give you the solution.
Below demo2 sample code shows you how to use an array statement to restructure your data from a wide to a long structure.
The demo1 sample code demonstrates how you would need to write such code without an array. I hope that explains to you the main purpose of a SAS array: It allows you to loop over variables without having to spell them out one by one. Just think how the demo1 code would need to look like if there were 40 variables.
data have;
input id var_1 $ var_2 $ var_3 $ var_4 $;
datalines;
1 A1 A2 A3 A4
2 B1 B2 B3 B4
;
data demo1;
set have;
var=var_1; output;
var=var_2; output;
var=var_3; output;
var=var_4; output;
drop var_1 - var_4;
run;
data demo2;
set have;
array vars {*} var_1 - var_4;
do i=1 to dim(vars);
var=vars[i];
output;
end;
drop i var_1 - var_4;
run;
Ahh, I see. I managed to code an array, but it outputs the scores for both Knee 1 and Knee 2.
Is there a way to implement a conditional statement, where it's like "if Knee_1or2 = 2, then do not include this row in the output?"
Ohp, I got it. Disregard!
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.