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

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. 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@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.

View solution in original post

2 REPLIES 2
cminard
Quartz | Level 8

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;
ballardw
Super User

@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.

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
  • 1693 views
  • 0 likes
  • 3 in conversation