Ankit
Ankit

Reputation: 75

Count variable in sas

I am wanting to set a count variable for another variable (innings). Additionally I want the count variable to reset every time the innings variables changes from 1 to 2. For example

innings  count
1         1
1         2
1         3
1         4
1         5
1         6
1         7
1         8
1         9
1         10
2         1
2         2
2         3
2         4
2         5
2         6
2         7
2         8
2         10
2         11
2         12
1         1
1         2
1         3
1         4
1         5
1         6

I have tried the following code:

data T20_SCORECARD_data_innings;
   set T20_SCORECARD_data_innings;
   count + 1;
   by innings;
   if first.innings then count = 0;
run;

But it doesn't seem to work.

Any help would be greatly appreciated.

Ankit

Upvotes: 0

Views: 414

Answers (2)

Reeza
Reeza

Reputation: 21294

If your data is truly not sorted and simply grouped into bins, 1 and 2 then you can use your code but add the NOTSORTED option to your BY statement.

data T20_SCORECARD_data_innings;
  set T20_SCORECARD_data_innings;
  by innings NOTSORTED;
  count + 1;
  if first.innings then count = 0;
run;

Upvotes: 2

DCR
DCR

Reputation: 15698

In a datastep when you use the by clause the data needs to be sorted. In your case it isn't. If you change your data so the third group (second group of 1's) to 3's your code should work.

Upvotes: 1

Related Questions