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

Hi....I am trying to do a count by two grouping variables and would like it to rest to a count of 1 when a new "Numb" occurs and not after a new "New_Dos" occurs. I am using a Data Step that generates the following output ( highlighted the counts =1 and should be 2):

 

DATA NEW;

SET NEW;

BY NUMB NEW_DOS;

COUNT=1;

IF NUMB = LAG(NUMB) AND NEW_DOS ^= LAG(NEW_DOS) THEN DO;

     COUNT=COUNT + 1;

     END;

RUN;

 

NUMB NEW_DOS COUNT
00147680 ML 1
00147680 ML 1
00147680 ML 1
00147680 W / V 2
00306819 W / V 1
00306819 W / V 1
00306819 W / W 2
00461024 % 1
00461024 ML 2
00532266 12 ML 1
00532266 ML 2
00773530 10 ML 1
00773530 10 ML 1
00773530 10 ML 1
00773530 10 ML 1
00773530 TAB 2
00773530 TAB 1
00773530 TAB 1
00816132 G 1
00816132 W / W 2
01914537 ML 1

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Ah, pop a retain in then:

data new;
  set new;
  by numb new_dos;
  retain count;
  if first.num then count=1;
  if numb=lag(numb) and new_dos ne lag(new_dos) then do;
    count=count+1;
  end;
run;

View solution in original post

10 REPLIES 10
RW9
Diamond | Level 26 RW9
Diamond | Level 26

To reset your variable on numb, then just do:

data new;
  set new;
  by numb new_dos;
  if first.num then count=1;
  if numb=lag(numb) and new_dos ne lag(new_dos) then do;
    count=count+1;
  end;
run;

Do note the updates I have made to the code, not using all uppercase, and using ne rather than ^=.

PaigeMiller
Diamond | Level 26

Why is ne preferred to ^= ???

--
Paige Miller
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Can you think of any occurence where "ne" could be misinterpreted?  ^ is power in most maths, and can be an escape char etc.  Personally I find the ne to be more readable, and that is my principal aim in programming.

PaigeMiller
Diamond | Level 26

@RW9 wrote:

Can you think of any occurence where "ne" could be misinterpreted?

No, I can't. Please enlighten me.

--
Paige Miller
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Thats my point, ne reads easier, and isn't used for anything else.

PaigeMiller
Diamond | Level 26

@RW9 wrote:

ne reads easier and isn't used for anything else.


My opinion is different. I have no problems seeing ^= in code and interpreting it or judging it to be readable.

--
Paige Miller
ballardw
Super User

Not to thread hijack but typos like =^ do not cause an error whereas using "en" instead of "ne" will:


data _null_;
input x y;
if x =^ y then put "is this expected for " x= y=;
datalines;
0 0
0 1
3 5
;
run;
twildone
Pyrite | Level 9

Hi RW9....Thanks for your help. I tried it and I get a 1 for the first Numb ( as from the first If Statement) and all the rest are empty blanks...

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Ah, pop a retain in then:

data new;
  set new;
  by numb new_dos;
  retain count;
  if first.num then count=1;
  if numb=lag(numb) and new_dos ne lag(new_dos) then do;
    count=count+1;
  end;
run;
MikeZdeb
Rhodochrosite | Level 12

Hi, this also works (lot less SAS code) ...

 

data y;
set x;
by num newdos;
newcount + first.newdos - first.num*newcount;
run;

 

take a look at ... Tips:Between and Within Group Counters

http://www.sascommunity.org/wiki/Tips:Between_and_Within_Group_Counters

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 2270 views
  • 0 likes
  • 5 in conversation