Controlling a first. counter with multiple "by" variables

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Controlling a first. counter with multiple "by" variables

[ Edited ]

 

Hi,

 

I have to implement a series of flags (I would also be happy using counters instead) (via a macro) to flag (count) unique occurrences of variables. The data must be sorted by variables 'a', 'b' and 'c' but I only want to increment the counter when a new instance of 'c' occurs within each 'a'. Is that possible? (specifically, my problem is that the 'c' flag = 1 every time I encounter a new 'b', which I don't want).

 

I suppose I could create a new sort variable that concatenates 'a' and 'b', but I was hoping there was a more elegant solution? 

 

data &dataset.;

  set &dataset.;

  by a b &c. ;

  if last.&c. then &c._flag = 1; else &c._flag = 0;

run;

 


Accepted Solutions
Solution
‎09-04-2017 09:42 PM
Super User
Posts: 21,464

Re: Controlling a first. counter with multiple "by" variables

First, get a non macro solution working. 

Second, post some example data please that illustrates your issue. 

It's sometimes hard to follow the logic in words, and sometimes the correct terms aren't used but having the data makes that easier to determine. 

 

You should be able to use IF first.C directly as far as I can tell, so not sure what B has to do with it, but an example will clarify that. 

 

If you're uncertain you can also review the examples in the BY group processing in the documentation.

View solution in original post


All Replies
Solution
‎09-04-2017 09:42 PM
Super User
Posts: 21,464

Re: Controlling a first. counter with multiple "by" variables

First, get a non macro solution working. 

Second, post some example data please that illustrates your issue. 

It's sometimes hard to follow the logic in words, and sometimes the correct terms aren't used but having the data makes that easier to determine. 

 

You should be able to use IF first.C directly as far as I can tell, so not sure what B has to do with it, but an example will clarify that. 

 

If you're uncertain you can also review the examples in the BY group processing in the documentation.

☑ This topic is solved.

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

Discussion stats
  • 1 reply
  • 130 views
  • 1 like
  • 2 in conversation