Using SAS 9.4
My data is all character (60 variables) except for the ID and the characters are either "checked" or "unchecked" as the response.
I have an array (see below) that I am trying to count every time a variable is "checked" by ID. The data set is unique so it is only one row per ID that I am trying to count. When I run the code the count continues down each row of the data set. I would appreciate any insight into how to correct the code. Thank you
data array(drop=i);
set check;
array b _character_;
by id;
do i = 1 to dim(b);
if b[i] =: 'C' then do;
if first.id then count + 1;
end;
end;
run;
Capture.PNG
It should be
ID Count
7162 2
7168 4
7169 4
7170 2
etc.
@GS2 wrote:
Using SAS 9.4
My data is all character (60 variables) except for the ID and the characters are either "checked" or "unchecked" as the response.
I have an array (see below) that I am trying to count every time a variable is "checked" by ID. The data set is unique so it is only one row per ID that I am trying to count. When I run the code the count continues down each row of the data set. I would appreciate any insight into how to correct the code. Thank you
data array(drop=i); set check; array b _character_; by id; do i = 1 to dim(b); if b[i] =: 'C' then do; if first.id then count + 1; end; end; run;
When ever you use the construct
then count + 1;
you have told SAS implicitly to retain the value. So possibly you just need to use: then count = sum(count,1);
Use of sum this way means that you do not need to initialize a value for count.
Maybe something like?
proc sort data=check; by id; run;
proc transpose data=check out=tcheck;
var var1-var60;
by id;
run;
data tcheck2;
set tcheck;
by id;
if first.id then counter=1*(col1='checked');
else counter+1*(col1='checked');
if last.id then output;
run;
@GS2 wrote:
Using SAS 9.4
My data is all character (60 variables) except for the ID and the characters are either "checked" or "unchecked" as the response.
I have an array (see below) that I am trying to count every time a variable is "checked" by ID. The data set is unique so it is only one row per ID that I am trying to count. When I run the code the count continues down each row of the data set. I would appreciate any insight into how to correct the code. Thank you
data array(drop=i); set check; array b _character_; by id; do i = 1 to dim(b); if b[i] =: 'C' then do; if first.id then count + 1; end; end; run;
When ever you use the construct
then count + 1;
you have told SAS implicitly to retain the value. So possibly you just need to use: then count = sum(count,1);
Use of sum this way means that you do not need to initialize a value for count.
CFC_SAS_Communities_400x225.jpg
It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.
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.