BookmarkSubscribeRSS Feed
knargis160
Calcite | Level 5

Hi 

I have dataset with 

 who_reacted_1   who_reacted_2----so on

 1.              5556                   5555       

 I want to calculate total number of who_reacted-if ID is 5555 

who_reacted_1

 

 

 

14 REPLIES 14
ed_sas_member
Meteorite | Level 14

Like this ?

 

data have;
	input id who_reacted_1 who_reacted_2  who_reacted_3  who_reacted_4 who_reacted_5;
	datalines;
1 5556 5555 5555 5557 5555
;
run;    

data want;
	set have;
	array who_reacted_(5);
	do i=1 to dim(who_reacted_);
		if who_reacted_(i) = 5555 then count+1;
	end;
	drop i;
run;
knargis160
Calcite | Level 5

Thanks!

Do you how to include multiple ID's in the statement below-5555,5556,5557,

I am getting an error.

 if who_reacted_(i) = 5555 then count+1;
end
;
drop I;

ed_sas_member
Meteorite | Level 14

Hi @knargis160 

 

Do you mean this ?

 

data have;
	input who_reacted_1 who_reacted_2  who_reacted_3  who_reacted_4 who_reacted_5;
	datalines;
5556 5555 5555 5557 5555
;
run;    


data want;
	set have;
	array who_reacted_(5);
	do i=1 to dim(who_reacted_);
		if who_reacted_(i) in (5555,5556,5557) then count+1;
	end;
	drop i;
run;
knargis160
Calcite | Level 5

@ed_sas_member 

This code works but no error in coding but I am not getting actual number of reactions instead it is count from 1 to 30 observations.

 

ed_sas_member
Meteorite | Level 14

Hi @knargis160 

Could you please share some sample data as well as the expected output?

knargis160
Calcite | Level 5

Hi sample data looks like this

 

input who_reacted_1 who_reacted_2  who_reacted_3  who_reacted_4 who_reacted_5.... who_reacted_40;
datalines;
5556 5555 5555 5557 5555

5556

5555 5556

5557

 

Output: Count

1

2

3... so on with code. It was working for single ID.

Does this makes more sense?

 

ed_sas_member
Meteorite | Level 14

Hi @knargis160 

 

Is this better ?

-> count will give you the number of 'columns' who-react that were in 555 556 and 5557 for each row.

data have;
	infile datalines dlm=" " dsd truncover;
	input who_reacted_1 who_reacted_2  who_reacted_3  who_reacted_4 who_reacted_5;
	datalines;
5556    
5555 5556 5554 5557
5557 5554
;
run;    

data want;
	set have;
	array who_reacted_(5);
	count=0;
	do i=1 to dim(who_reacted_);
		if who_reacted_(i) in (5555,5556,5557) then count+1;
	end;
	drop i;
run;
knargis160
Calcite | Level 5

Thanks it worked out

knargis160
Calcite | Level 5

I want to transpose the data

PTID who_reacted_1 who_reacted_2  who_reacted_3  who_reacted_4 who_reacted_5;
1 5556   
2 5555 5556 5554 5557
3. 5557 5554

 

I want to have the data like

1. 5556

2. 5555

2. 5556

2.5554

2. 5557

Can you suggest a best way to figure that out?

Tom
Super User Tom
Super User

So if you have data like:

data have ;
  input PTID who_reacted_1-who_reacted_5;
cards;
1 5556 . . . .   
2 5555 5556 5554 5557 .
3 5557 5554 . . . 
;

You just need to use BY statement in the PROC transpose.  To get rid of the empty cells you can use a WHERE= dataset option on the output dataset.

proc tranpose data=have out=want (where=(not missing(col1));
  by ptid;
  var who_reacted_1-who_reacted_5;
run;
knargis160
Calcite | Level 5

Some how I am having col2, col3 as well. Do you know why

Tom
Super User Tom
Super User

@knargis160 wrote:

Some how I am having col2, col3 as well. Do you know why


Your BY variables do not uniquely identify the observations in your original dataset.  You might have to add a new variable first if there is no combination of variables that work.  Note you could actually use your first analysis variable and then just drop it from the output dataset.

 

Or just use the ARRAY method.

 data want;
  set have;
  array who_reacted_[5]  ;
  do idx=1 to dim(who_reacted_);
    value = who_reacted_[idx] ;
    if not missing(value) then output;
  end;
run;
knargis160
Calcite | Level 5

Thanks it worked!

Do you know if I can categorize in these ID's in array statement

Tom
Super User Tom
Super User

How did you get counts of 1,2,3?  I see counts of 5,1,2,1.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 14 replies
  • 1124 views
  • 0 likes
  • 3 in conversation