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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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