BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
smithboy
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

2 REPLIES 2
Haikuo
Onyx | Level 15

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

smithboy
Calcite | Level 5

Thanks,

That worked perfectly...

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 2 replies
  • 656 views
  • 1 like
  • 2 in conversation