Help using Base SAS procedures

DO UNTIL WITHIN A BY GROUP

Reply
Occasional Contributor
Posts: 18

DO UNTIL WITHIN A BY GROUP

Please help...I am trying to run a loop and output all records within the by group until the runbal is less than or equal to 0. Obs in bold is what I want to output.

Here is the abbreviated dataset:
custid$ oblgid$ runbal
1234 A 1536
1234 A 0

1234 A 1536 (do not want this one to output)
1235 A 0
1236 A 500
1236 A 0

1236 A -500 (do not want this one to output)

Here is what I have so far, but can't seem to think of a way to get the right output. If I do a DO UNTIL 0 then it only outputs one row.

DATA MAXOBS_GT2_PAYDOWN;

SET MAXOBS_GT2;
BY CUSTID OBLGID;
RETAIN RUNBAL;
IF FIRST.OBLGID THEN DO;
RUNBALTEMP=CURRBALAMT;
RUNBAL=RUNBALTEMP;
END;
RUNBALTEMP=RUNBAL;
RUNBAL=RUNBALTEMP-PAID;

RUN;

Any help would be greatly appreciated.
Super Contributor
Super Contributor
Posts: 3,174

Re: DO UNTIL WITHIN A BY GROUP

In the DATA step, use an OUTPUT statement, making it's execution conditional on your required IF/THEN condition, such as:

IF A LT TOT_A THEN OUTPUT;
Occasional Contributor
Posts: 18

Re: DO UNTIL WITHIN A BY GROUP

Thanks...

But I am not sure it will work since I don't always want to output if the condition is true. I want to output the obs until the value gets to zero (also output the zero value as well). The following obs after the value is zero, I want to exclude.
Super Contributor
Super Contributor
Posts: 3,174

Re: DO UNTIL WITHIN A BY GROUP

Suggest you try some IF / THEN coding in your DATA step - add self-checking diagnostic statement such as:

PUTLOG '>DIAG> I am here.' / _ALL_;

With this additional log output, you will see each SAS variable value as your DATA step iterates -- you may need to put in multiple PUTLOG statements to experiment with your program.

I suspect that you can address your "conditional output" scenario with some statement in the form:

IF () THEN OUTPUT;


and you may need to keep a running SAS temporary numeric variable, either using possibly some combination of accumulation with a RETAIN and then DROP the temp variable.

Scott Barry
SBBWorks, Inc.
Ask a Question
Discussion stats
  • 3 replies
  • 119 views
  • 0 likes
  • 2 in conversation