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

 

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

1 REPLY 1
Reeza
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1 reply
  • 329 views
  • 1 like
  • 2 in conversation