BookmarkSubscribeRSS Feed
septemberbulb
Obsidian | Level 7

Hello,

I am trying to count the number of persons based on Variable Account All below:

 #
1 Person X
2 Person Y
3+ Person Z

 

Data is like this layout:  

If there is a 2 duplicate account all, it means it has 2 personal ID (vertical view)

if 3 duplicates, then 3 personal ID,

I am not sure how to count this automatically in this situation? 

Could anyone help? 

   Thank you

Account AllAccount IDPerson ID
1231231234
4564564568
4564564569
78978978910
78978978911
78978978912
1011231011231011213
12131412131412131415
131415..

 

2 REPLIES 2
A_Kh
Lapis Lazuli | Level 10

You can process data using BY -group in data step, where first.variable  and last.variable determine the start and the end counts of your accounts (running counts).  

data have;
input Account_All	Account_ID	Person_ID;
cards; 
123	123	1234
456	456	4568
456	456	4569
789	789	78910
789	789	78911
789	789	78912
101123	101123	1011213
121314	121314	12131415
131415	.	.
;
proc print;run; 

data want; 
	set have;
	by account_all account_id;
	if first.account_id then Total_person=1;
	else Total_person+1; 
proc print;run; 

 In case you need only totals by accoint_id, use proc sql;

proc sql;
	create table want as
		select*, count(*) as Total_person
			from have
		group by account_all, account_id;
quit;
Reeza
Super User

Please, when you post a problem, make sure the solution shown aligns with the data shown. So for the sample data shown, what exactly would be the expected output. 


I'm interpreting the question differently than @A_Kh for example. 

 

data have;
input Account_All   Account_ID  Person_ID;
cards; 
123 123 1234
456 456 4568
456 456 4569
789 789 78910
789 789 78911
789 789 78912
101123  101123  1011213
121314  121314  12131415
131415  .   .
;

proc format;
value dup_format
1  = '1'
2 = '2'
3 - high = '3+';
run;

proc sql;
create table temp as
select account_all, count(distinct person_id) as unique_persons
from have
group by account_all;
quit;

proc freq data=temp;
table unique_persons;
format unique_persons dup_format.;
run;

Results:

 

Reeza_0-1685478243192.png


@septemberbulb wrote:

Hello,

I am trying to count the number of persons based on Variable Account All below:

  #
1 Person  X
2 Person  Y
3+ Person  Z

 

Data is like this layout:  

If there is a 2 duplicate account all, it means it has 2 personal ID (vertical view)

if 3 duplicates, then 3 personal ID,

I am not sure how to count this automatically in this situation? 

Could anyone help? 

   Thank you

Account All Account ID Person ID
123 123 1234
456 456 4568
456 456 4569
789 789 78910
789 789 78911
789 789 78912
101123 101123 1011213
121314 121314 12131415
131415 . .

 


 

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 25. 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
  • 2 replies
  • 367 views
  • 0 likes
  • 3 in conversation