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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 10 replies
  • 1073 views
  • 0 likes
  • 5 in conversation