Hi,
I have a table as follows where score has a value from 0 to 4, and corresponding to each value of the score I have a column for risk.
ID Score Risk0 Risk1 Risk2 Risk3 Risk4
12 0 0.1 0.5 0.6 0.2 0.12
15 3 0.4 0.6 0.7 0.1 0.39
10 1 0.2 0.3 0.4 0.5 0.67
I want to have a new variable like Risk where its value depends on Score. For example for observation with ID=12, since Score=0, I want to have Risk=Risk0=0.1, similarly for ID=10, Risk should be Risk=Risk1=0.3.
Thanks!
Like this?
data have;
input ID Score Risk0 Risk1 Risk2 Risk3 Risk4;
datalines;
12 0 0.1 0.5 0.6 0.2 0.12
15 3 0.4 0.6 0.7 0.1 0.39
10 1 0.2 0.3 0.4 0.5 0.67
;
data want;
set have;
array _ {0:4} Risk0-Risk4;
Risk = _ [Score];
run;
Result:
ID Score Risk0 Risk1 Risk2 Risk3 Risk4 Risk 12 0 0.1 0.5 0.6 0.2 0.12 0.1 15 3 0.4 0.6 0.7 0.1 0.39 0.1 10 1 0.2 0.3 0.4 0.5 0.67 0.3
Hi @Bright
Here is an attempt to do this, using an array:
data want;
set have;
array _Risk(0:4) Risk0-Risk4;
Risk = _Risk(score);
run;
It sounds like you want something like
data have; input ID Score Risk0 Risk1 Risk2 Risk3 Risk4 ; datalines; 12 0 0.1 0.5 0.6 0.2 0.12 15 3 0.4 0.6 0.7 0.1 0.39 10 1 0.2 0.3 0.4 0.5 0.67 ; data want; set have; array r(0:4) risk0-risk4; Risk=r[score]; run;
Often when you want to treat a group of variables in some manner an array is appropriate. The definition of this array says that the index values start at 0, which is one of your score values. So r[0] references Risk0, r[1] is Risk1 etc. Then use the Score as the index value to access the desired variable.
data have;
input ID Score Risk0 Risk1 Risk2 Risk3 Risk4;
datalines;
12 0 0.1 0.5 0.6 0.2 0.12
15 3 0.4 0.6 0.7 0.1 0.39
10 1 0.2 0.3 0.4 0.5 0.67
;
data want;
set have;
Risk=vvaluex(cats('risk',Score));
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.