DATA Step, Macro, Functions and more

Making COUNTER as a field by do loop – not working properly

Reply
Occasional Contributor
Posts: 9

Making COUNTER as a field by do loop – not working properly

Here is the basic structure of my code:
DATA SUSBUILD0;
SET SUSBUILD0;
LENGTH COUNTER 2;
COUNTER eq 1;
LAGPID eq LAG(PID);
LAGBENTYPE eq LAG(BEN_TYPE_CODE);
IF _N_ GT 1 THEN
DO;
IF (LAGPID eq PID AND
LAGBENTYPE eq BEN_TYPE_CODE)
THEN
COUNTER eq COUNTER + 1;
END;
RETURN;
Looks like this do loop can be only visited once and stop or somehow as it loops through, it always carries the old memory that COUNTER eq 1, so COUNTER becomes 1 + 1 eq 2. As a matter of fact, none of my output records have COUNTER carry values either than 1 or 2.
I have a feeling that there must be something simple I have missed in my do loop. Sorry for I asked such a dump question.
Valued Guide
Posts: 2,177

Re: Making COUNTER as a field by do loop – not working properly

Posted in reply to willow2010
change the COUNTER+1 statement from[pre] counter= counter+1;[/pre] to[pre] Counter+1;[/pre] there seem to be many other features in the code you posted that I have not seen among the examples available in SAS documentation on-line.
Why do you use EQ for assignment rather than the symbol "=" ?
hth
peterC
Super Contributor
Posts: 578

Re: Making COUNTER as a field by do loop – not working properly

Posted in reply to willow2010
If you want to maintain the value of COUNTER over multiple iterations of the data step, then you need to add the RETAIN COUNTER statement.
Contributor
Posts: 32

Re: Making COUNTER as a field by do loop – not working properly

Using the nomenclature Counter+1 (vs. counter = counter + 1) is an implicit retain.
Occasional Contributor
Posts: 14

Re: Making COUNTER as a field by do loop – not working properly

Hi,

In the case you still have the problem unsolved, it will be good to explain the purpose of the code (you are saying something in the subject line) and provide a sample of the data. Also the log could be useful.

Warm regards,
Vasile
Occasional Contributor
Posts: 9

Re: Making COUNTER as a field by do loop – not working properly

Posted in reply to willow2010
Thank you for all of your joint efforts. Now my code works!
DATA SUSBUILD0;
SET SUSBUILD0;
LENGTH COUNTER 2;
LAGPID=LAG(PID);
LAGBENTYPE=LAG(BEN_TYPE_CODE);
IF _N_ GT 1 THEN
DO;
IF (LAGPID = PID AND
LAGBENTYPE = BEN_TYPE_CODE)
THEN
COUNTER + 1;
ELSE
COUNTER = 1;
END;
RETURN;
The reason for me to put eq instead of = before was in concern whether = will be read as HTML tag, obviously not, it if the GT sign I need to be careful.

Cheers to you all!
Occasional Contributor
Posts: 9

Re: Making COUNTER as a field by do loop – not working properly

Posted in reply to willow2010
Ooh, I forgot to put the line RETAIN COUNTER 1; just under LENGTH COUNTER 2 to get the very first record works.

: )
Ask a Question
Discussion stats
  • 6 replies
  • 265 views
  • 0 likes
  • 5 in conversation