Contributor
Posts: 37

# help with if then statement

The purpose of the code is to determine when (yrmth) a person reaches \$100 in claims. In the sample data when the code processes the last row it goes into the "else" part but it has lost the total from the previous pass so I get total as missing (.). What am I doing wrong?

data

claims1;

input

indvid paid yrmth;

datalines

;

1 50 201101

2 25 201102

2 85 201103

;

proc sort data=claims1;

by indvid yrmth;

run;

data claims2 claimsmth1;

set claims1;

output claims2;

if indvid ne lag(indvid) then do;

total=0;

total=total+paid;

if total>=100 then output claimsmth1;

end;

else do;

total=total+paid;

if total>=100 then output claimsmth1;

end;

run;

I wasn't able to paste my code in here. It kept not pasting half the code so I had to type it. If there are any punctuation errors please disregard. The code does run.

Thanks,

DanD

PROC Star
Posts: 8,164

## help with if then statement

I don't understand why you want to output claims2 as it will merely be a copy of claims1.  What you were missing was a retain statement for total or, as I used below, a form that inherently retains the variable:

data claims1;

input indvid paid yrmth;

datalines;

1 50 201101

2 25 201102

2 85 201103

;

proc sort data=claims1;

by indvid yrmth;

run;

data claims2 claimsmth1;

set claims1;

by indvid;

output claims2;

if first.indvid then total=0;

total+paid;

if total>=100 then output claimsmth1;

run;

Contributor
Posts: 37

## Re: help with if then statement

Thanks for the reply art. That is a much simpler solution.

DanD

Frequent Contributor
Posts: 104

## Re: help with if then statement

1) Since the dataset is sorted, using first.indvid and last.indvid is easier and cleaner than using lag().

2) the total variable needs to be retained across observations via "retain total;" or using the "a + b;" sum statement.

You're probably looking for something like:

data claims2 claimsmth1;

set claims1;

by indvid yrmth;

output claims2;                             /* replication of claims1 */

if first.indvid then total = 0;

total = total + paid;     retain total 0;  /*  or you can use "total + paid;" which has an implied retain  */

if total>=100 then output claimsmth1;

run;

Contributor
Posts: 37

## Re: help with if then statement

Thanks for the advice on using retain DLing. I'll try that.

DanD

Valued Guide
Posts: 2,191

## help with if then statement

to reduce to the most basic elements, and return a data set with indvid, and date at which "paid" passes 100, try

data;

Do until( last.indvid );

set claims1;

by indvid;

TotPaid = sum( TotPaid, paid );

If totPaid GT 100 and missing( pass100 ) then pass100 = yrmth;

End;

Keep indvid pass100 totPaid yrmth;

Run;

Contributor
Posts: 37

## help with if then statement

Thanks for the alternative method Peter.

DanD

Discussion stats
• 6 replies
• 329 views
• 0 likes
• 4 in conversation