BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rj438
Calcite | Level 5

I have a question on something I know how to do the long way but am looking for more efficient code. 

IDABCDEFGStatus
10220102Intermed
22212221Ideal
32000112Poor
41222221Ideal

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!

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20
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
ballardw
Super User

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1103 views
  • 0 likes
  • 3 in conversation