DATA Step, Macro, Functions and more

Creating a count that starts over at 0 or a new group

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Creating a count that starts over at 0 or a new group

I have been trying (unsuccessfully) to create a variable that counts upward the number of periods a company is doing business in a particular market, which starts over at 1 when sales fall to $0, or when the market changes for the firm, or when firms change.  Any help would be appreciated.  The count variable below is what I am shooting for.

Firm     Market     Sales     Count

1          100          $354     1

1          100          $344     2

1          100          $244     3

1          100          $37       4

1          100          $0         .

1          200          $98       1

1          200          $99       2

1          200          $0         3

1          200          $34       1

1          200          $25       2

2          100          $0         .

2          100          $34       1

2          100          $44       2

2          100          $39       3

2          100          $0         .

2          200          $98       1

2          200          $99       2

2          200          $23       3

2          200          $34       4

2          200          $25        5

3          200          $9         1

3          200          $9         2

3          200          $2         3

3          200          $3         4

3          200          $2         5


Accepted Solutions
Solution
‎05-19-2013 11:00 PM
Respected Advisor
Posts: 3,124

Re: Creating a count that starts over at 0 or a new group

data have;

input Firm  :$   Market :$     Sales :dollar8. ;

cards;

1          100          $354   

1          100          $344   

1          100          $244   

1          100          $37    

1          100          $0     

1          200          $98    

1          200          $99    

1          200          $0     

1          200          $34    

1          200          $25    

2          100          $0     

2          100          $34    

2          100          $44    

2          100          $39    

2          100          $0     

2          200          $98    

2          200          $99    

2          200          $23    

2          200          $34    

2          200          $25    

3          200          $9     

3          200          $9     

3          200          $2     

3          200          $3     

3          200          $2     

;

data want;

  set have;

    by firm market notsorted;

    if first.market then call missing (count);

  count+1;

  if sales=0 then call missing(count);

  run;

Haikuo

View solution in original post


All Replies
Solution
‎05-19-2013 11:00 PM
Respected Advisor
Posts: 3,124

Re: Creating a count that starts over at 0 or a new group

data have;

input Firm  :$   Market :$     Sales :dollar8. ;

cards;

1          100          $354   

1          100          $344   

1          100          $244   

1          100          $37    

1          100          $0     

1          200          $98    

1          200          $99    

1          200          $0     

1          200          $34    

1          200          $25    

2          100          $0     

2          100          $34    

2          100          $44    

2          100          $39    

2          100          $0     

2          200          $98    

2          200          $99    

2          200          $23    

2          200          $34    

2          200          $25    

3          200          $9     

3          200          $9     

3          200          $2     

3          200          $3     

3          200          $2     

;

data want;

  set have;

    by firm market notsorted;

    if first.market then call missing (count);

  count+1;

  if sales=0 then call missing(count);

  run;

Haikuo

New Contributor
Posts: 2

Re: Creating a count that starts over at 0 or a new group

Thanks,

That worked perfectly...

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 151 views
  • 1 like
  • 2 in conversation