DATA Step, Macro, Functions and more

summing on character variables

Accepted Solution Solved
Reply
Contributor
Posts: 52
Accepted Solution

summing on character variables

Hello everyone, 

 

I'm trying to sum on character variables and I'm trying to think about the best way to approach this goal. Let me sketch the rough structure of my data. 

 

ID       Country 

 

1      United States 

1      United States

1      France 

1      Canada

2      South Africa

2      Japan 

2      Japan 

2      Japan

3      United States

3      Canada

3      Canada

3      Canada

 

 

I want the output to show me how many of each country appears for each ID, like so :

ID   Country       numberofoccurence

1 United States    2

1 Canada            1 

1 France             1

2 South Africa     1

2 Japan              3

3 United States  1 

3 Canada           3 

 

 

 

How can I accomplish this? 

 

As always, thanks for your help. 


Accepted Solutions
Solution
‎02-20-2017 12:37 AM
PROC Star
Posts: 1,167

Re: summing on character variables

Nice and easy with PROC SQL:

 

proc sql;

create table want as

select ID, Country, count(*) as numberofoccurence

from have group by ID, Country;

View solution in original post


All Replies
Respected Advisor
Posts: 4,173

Re: summing on character variables

There are multiple SAS Proc's you could use for this task.

I'd probably go for Proc SQL with a count(*) in the Select statement and a Group By ID, Country

 

Solution
‎02-20-2017 12:37 AM
PROC Star
Posts: 1,167

Re: summing on character variables

Nice and easy with PROC SQL:

 

proc sql;

create table want as

select ID, Country, count(*) as numberofoccurence

from have group by ID, Country;

Regular Contributor
Posts: 228

Re: summing on character variables

Or this? proc freq data=have noprint; tables id*country / out=want(drop=percent rename=count=numberofoccurence); run; 

Super User
Posts: 19,869

Re: summing on character variables

You aren't summing you're counting the number of occurrences by a categorical variable. I'm only clarifying the term so that you can search for valid solutions. Key word searches requires the correct key words and are primarily designed for English speakers. 

Contributor
Posts: 52

Re: summing on character variables

Thank you all very much for your help!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 243 views
  • 7 likes
  • 5 in conversation