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

data ds;
infile datalines;
input name$ a$ b$ c$ d$ e$ f$;
datalines;
vnod yes no no no no yes
jag no yes no yes no yes
raki yes yes yes yes yes no
;
run;

data ds;
infile datalines;
input name$ a$ b$ c$ d$ e$ f$;
datalines;
vnod yes no no no  no yes
jag  no  yes no yes no yes
raki yes yes yes yes yes no
;
run;

i need this dataet as output:

 name    a      b     c     d      e    f       count(yes)
vnod   yes    no    no   no   no   yes     2
jag      no     yes    no  yes   no  yes    3
raki     yes   yes   yes  yes  yes  no     5

 

i need to create a column count the  row wise 'yes' from a to f columns.  kindly help me.

Thank  you  

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

Or:

data WANT;
  set HAVE;
  COUNT=countw(catx(' ',A,B,C,D,E,F), 'yes');
run;

 

View solution in original post

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

One way

 

data want(drop=i);  
   set ds;
   array MyArray $ a--f;
   count=0;
   do i=1 to dim(MyArray);
      if MyArray[i]='yes' then count+1;
   end;
run;
r_behata
Barite | Level 11
data want;
	set ds;

	array cnt{*} a--f;

	count=count(catx('',of cnt[*]),'yes');

run;
ChrisNZ
Tourmaline | Level 20

Or:

data WANT;
  set HAVE;
  COUNT=countw(catx(' ',A,B,C,D,E,F), 'yes');
run;

 

vinod4842
Fluorite | Level 6
its not working correctly friend as this first variable row value is not correct
ChrisNZ
Tourmaline | Level 20

Oh I misrepresented COUNTW().

This works.

data ds;
  infile datalines;
  input NAME$ A$ B$ C$ D$ E$ F$;
  COUNT=count(catx(' ',of A--F), 'yes'); putlog COUNT=;
datalines;
vnod yes no no no  no yes
jag  no  yes no yes no yes
raki yes yes yes yes yes no
run;

COUNT=2
COUNT=3
COUNT=5

 

 

ChrisNZ
Tourmaline | Level 20

Which does the same as

 

 COUNT=count(catx(' ',A,B,C,D,E,F), 'yes'); putlog COUNT=;

 

 

ChrisNZ
Tourmaline | Level 20

Thread title changed from "sas" to "count values in an observation"

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 7 replies
  • 951 views
  • 0 likes
  • 4 in conversation