BookmarkSubscribeRSS Feed
swioak
Fluorite | Level 6

Do I need to calculate the daily counts of a unique ID by group or can I do a frequency of group and divide by the number of days of my cohort?

Simple dat example:

ID     GROUP   DATE

1        AAA        1/1/23

2       AAA        1/1/23

3       AAA        1/10/23

4       BBB         1/1/23

5       BBB         1/2/23

5       BBB         1/3/23

Desired output:

GROUP        Average

AAA              0.3

BBB               1.0

4 REPLIES 4
mkeintz
PROC Star

@swioak wrote:

Do I need to calculate the daily counts of a unique ID by group or can I do a frequency of group and divide by the number of days of my cohort?

How can respondents tell you how to do something, if you don't state what the objective is?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

Here is a working data step to provide that data. You should provide your data in this form so we do not have to guess about things like variable types.

data have;
  input ID     GROUP $   DATE :mmddyy10.;
  format date mmddyy10.;
datalines;
1        AAA        1/1/23
2       AAA        1/1/23
3       AAA        1/10/23
4       BBB         1/1/23
5       BBB         1/2/23
5       BBB         1/3/23
;

Now, walk us through exactly what you do to arrive at those two "averages" especially for group AAA.

Be prepared to carry more decimals than you think you need.

 


@swioak wrote:

Do I need to calculate the daily counts of a unique ID by group or can I do a frequency of group and divide by the number of days of my cohort?

Simple dat example:

ID     GROUP   DATE

1        AAA        1/1/23

2       AAA        1/1/23

3       AAA        1/10/23

4       BBB         1/1/23

5       BBB         1/2/23

5       BBB         1/3/23

Desired output:

GROUP        Average

AAA              0.3

BBB               1.0


 

Ksharp
Super User
/*Assuming I understood what you mean.
And use the dataset @ballardw posted*/
data have;
  input ID     GROUP $   DATE :mmddyy10.;
  format date mmddyy10.;
datalines;
1        AAA        1/1/23
2       AAA        1/1/23
3       AAA        1/10/23
4       BBB         1/1/23
5       BBB         1/2/23
5       BBB         1/3/23
;
proc sql;
create table want as
select group,count(*)/(range(date)+1) as Avergae
 from have 
  group by group;
quit;
mkeintz
PROC Star

A data step solution would look like:

 

data have;
  input ID     GROUP $   DATE :mmddyy10.;
  format date mmddyy10.;
datalines;
1        AAA        1/1/23
2       AAA        1/1/23
3       AAA        1/10/23
4       BBB         1/1/23
5       BBB         1/2/23
5       BBB         1/3/23
;

data want (keep=id average);
  set have ;
  by group;

  date_range+dif(date);
  if first.group then call missing(date_range,n_ids);
  n_ids+1;
  if last.group;
  average=n_ids/(date_range+1);
run;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 913 views
  • 2 likes
  • 4 in conversation