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: 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
  • 4 replies
  • 753 views
  • 2 likes
  • 4 in conversation