I realize this post is 3 years-old (and the original poster had already resolved the issue him/herself), but I have found myself in a similar situation, stumbling across a SAS community question that perfectly replicates the question I'm trying to sort out, with no final resolution. Just in case others are struggling to find syntax to create a count variable, here is some code to help: First, sort data by your grouping variable and the variable you want counted. ID = grouping variable; Date = ordering variable proc sort data=one;
by ID Date;
run; Next, create a count variable in a new data set. In this case, each first ID starts at a count of 1, and continues to count up until the next ID. data two;
set one;
*Create count variable.;
Count + 1;
by ID;
if first.ID then Count = 1;
run; If you wanted to create a running count within more than one category, for example, number of observations within each participant and date, you can simply add the additional variable to your count syntax after the "by" statement (e.g., "by ID Date"). If you need something slightly more complicated, for example, counting only certain types of observations, some additional syntax is listed below. data two;
set one;
*Create drinking observation count.;
if Drinking=1 then Count + 1;
by ID;
if first.ID then Count = 1;
run; Here, I created a count of drinking observations (only when drinking = 1, not drinking = 0) within each participant ID.
... View more