BookmarkSubscribeRSS Feed
awc380
Calcite | Level 5
Hi!

I'm trying to generate a specific 'count' variable (I know...aren't we all), and I know how to do basic count variable (as below), but I want the variable 's_count' to equal the total count for each observation, generated by 'id'.

I can get part of the way there, but I need the 's_count' variable to have no missing observations. i.e.: in the chart below, 's_count' SHOULD read (1,2,2,3,3,3) - I need 's_count' to show the number of observations having the same id number. (A sample fraction, of sorts.)

num id count s_count

1 a 1 1
2 b 1 .
3 b 2 2
4 c 1 .
5 c 2 .
6 c 3 3

This was the best I could do. Here was my data step:

data ds2;
set ds1;
count + 1;
by id;
if first.id then count = 1;
if last.id then s_count = count;
run;

Let me know if I'm being entirely unclear...
2 REPLIES 2
deleted_user
Not applicable
Many ways to do this. Simplest would be:

proc sql;
create table tot_count as
select *, max(count) as s_count
from ds2
group by id;
quit;

ie dont do the s_count in the datastep.

even easier:

proc sql;
create table ds2 as
select *, count(*) as s_count
from ds1
group by id;
quit;

from the basic data.
awc380
Calcite | Level 5
Great! Worked perfectly.
Thanks a lot.

Andy

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5072 views
  • 0 likes
  • 2 in conversation