BookmarkSubscribeRSS Feed
willow2010
Calcite | Level 5
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.
6 REPLIES 6
Peter_C
Rhodochrosite | Level 12
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
DBailey
Lapis Lazuli | Level 10
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.
DavidJ
Calcite | Level 5
Using the nomenclature Counter+1 (vs. counter = counter + 1) is an implicit retain.
Vasile01
Fluorite | Level 6
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
willow2010
Calcite | Level 5
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!
willow2010
Calcite | Level 5
Ooh, I forgot to put the line RETAIN COUNTER 1; just under LENGTH COUNTER 2 to get the very first record works.

: )

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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