I have a question on something I know how to do the long way but am looking for more efficient code.
ID | A | B | C | D | E | F | G | Status |
1 | 0 | 2 | 2 | 0 | 1 | 0 | 2 | Intermed |
2 | 2 | 2 | 1 | 2 | 2 | 2 | 1 | Ideal |
3 | 2 | 0 | 0 | 0 | 1 | 1 | 2 | Poor |
4 | 1 | 2 | 2 | 2 | 2 | 2 | 1 | Ideal |
I have 7 variables (A-G) that are all coded 0, 1 or 2. I need to categorize participants based on whether at least 5 to 7 of these variables = 2 (ideal) vs. 3-4 (intermediate) vs. 0-2 (poor), aka the Status variable on the table. What is the best way to do this?
Thanks for any help!
Since it appears that you actually want to count the number of two's that appear, if I understand then something like this will do the counting:
data have; input ID A B C D E F G ; datalines; 1 0 2 2 0 1 0 2 2 2 2 1 2 2 2 1 3 2 0 0 0 1 1 2 4 1 2 2 2 2 2 1 ; data want; set have; Numberof2 = countc(cats(a,b,c,d,e,f,g),'2'); run;
You could use a custom format to assign text based on the value of Numberof2 or an it/then/else block to create a text variable.
Please note the data step to provide something code can be run against.
proc format lib=work;
value grp
0-2='Poor'
3-4='Intermed'
5-7='Ideal'
other='Undefined'
;
data have;
input ID A B C D E F G Status $;
cards;
1 0 2 2 0 1 0 2 Intermed
2 2 2 1 2 2 2 1 Ideal
3 2 0 0 0 1 1 2 Poor
4 1 2 2 2 2 2 1 Ideal
;
data want;
set have;
array t A B C D E F G;
status=put(countc(cats(of t(*)),'2'),grp.);
run;
proc print noobs;run;
ID | A | B | C | D | E | F | G | Status |
---|---|---|---|---|---|---|---|---|
1 | 0 | 2 | 2 | 0 | 1 | 0 | 2 | Intermed |
2 | 2 | 2 | 1 | 2 | 2 | 2 | 1 | Ideal |
3 | 2 | 0 | 0 | 0 | 1 | 1 | 2 | Poor |
4 | 1 | 2 | 2 | 2 | 2 | 2 | 1 | Ideal |
Since it appears that you actually want to count the number of two's that appear, if I understand then something like this will do the counting:
data have; input ID A B C D E F G ; datalines; 1 0 2 2 0 1 0 2 2 2 2 1 2 2 2 1 3 2 0 0 0 1 1 2 4 1 2 2 2 2 2 1 ; data want; set have; Numberof2 = countc(cats(a,b,c,d,e,f,g),'2'); run;
You could use a custom format to assign text based on the value of Numberof2 or an it/then/else block to create a text variable.
Please note the data step to provide something code can be run against.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.