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
Or:
data WANT;
set HAVE;
COUNT=countw(catx(' ',A,B,C,D,E,F), 'yes');
run;
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;
data want;
set ds;
array cnt{*} a--f;
count=count(catx('',of cnt[*]),'yes');
run;
Or:
data WANT;
set HAVE;
COUNT=countw(catx(' ',A,B,C,D,E,F), 'yes');
run;
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
Which does the same as
COUNT=count(catx(' ',A,B,C,D,E,F), 'yes'); putlog COUNT=;
Thread title changed from "sas" to "count values in an observation"
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.