Hello.
I have a dataset with 16 variables where subjects rate their symptoms (values from 0 to 10).
Now, for each subject, I need to use SAS code to identify the 5 variables where they rated their symptoms the highest. I need to perform a calculation for each subject using those 5 variables only.
How would I do this?
I appreciate any help that you can provide.
Thanks!
This is really going to need some example starting data, fake is fine as long as it is representative of what you have, and what you expect the result to look like afterward.
What KIND of calculation do you mean to do?
This code will take the 5 largest values from 16 variables v1-v16 (since you provided no variable names replace with your variables) and place them from largest to smallest in variables L1-L5;
data want; set have; array v v1-v16; array l{5}; do i=1 to dim(l); l[i] = largest(i,of v(*)); end; run;
If you need to know which variable contributed which value you need to provide more information on what you are doing.
First of all, provide some example data to get a usable code answer.
Secondly, what do you mean by "where they rated their symptoms the highest"? Does highest mean the sum of the entire variable or?
It depends on your data structure.
Can you show a sample of what you have and what you expect as output?
This is really going to need some example starting data, fake is fine as long as it is representative of what you have, and what you expect the result to look like afterward.
What KIND of calculation do you mean to do?
This code will take the 5 largest values from 16 variables v1-v16 (since you provided no variable names replace with your variables) and place them from largest to smallest in variables L1-L5;
data want; set have; array v v1-v16; array l{5}; do i=1 to dim(l); l[i] = largest(i,of v(*)); end; run;
If you need to know which variable contributed which value you need to provide more information on what you are doing.
What kind of calculation would you do on ordinal variables?
If you need the variables that instead of the values, then i suggest transpose the dataset and sort descending and take the first 5 values.
data test ;
input val1-val10;
datalines;
1 2 3 4 5 6 7 8 9 10
;
run;
proc transpose data=test out=tr ;
run;
proc sort data=tr;
by descending col1;
run;
data var_max;
set tr;
if _n_<=5;
run;
Please provide more information to be more specific;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.