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

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
TomKari
Onyx | Level 15

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

5 REPLIES 5
Patrick
Opal | Level 21

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

 

TomKari
Onyx | Level 15

Nice and easy with PROC SQL:

 

proc sql;

create table want as

select ID, Country, count(*) as numberofoccurence

from have group by ID, Country;

Miracle
Barite | Level 11

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

Reeza
Super User

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. 

r4321
Pyrite | Level 9

Thank you all very much for your help!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 5 replies
  • 6070 views
  • 7 likes
  • 5 in conversation