I have a input like below:
loanno deb cred id
1234 27199 0 R
1234 27199 0 R
1234 0 27199 P
1234 0 27199 P
567 2000 0 R
567 2000 0 R
567 0 2000 P
I need ouput like
loanno deb cred id flag
1234 27199 0 R equal
1234 0 27199 P equal
1234 27199 0 R equal
1234 0 27199 P equal
567 2000 0 R equal
567 0 2000 P equal
567 2000 0 R not equal
Please help me .......Thanks in advance
data have;
input loanno deb cred id $;
cards;
1234 27199 0 R
1234 27199 0 R
1234 0 27199 P
1234 0 27199 P
567 2000 0 R
567 2000 0 R
567 0 2000 P
;
data need;
do _n_=1 by 1 until(last.loanno);
set have;
by loanno notsorted;
length flag $7;
if first.loanno and not last.loanno then flag='equal';
else if mod(_n_,2) ne 0 and last.loanno then flag='Unequal';
if mod(_n_,2)=0 then do;
cred=max(deb,cred);
deb=0;
end;
else do;
deb=max(deb,cred);
cred=0;
end;
output;
end;
run;
Check this solution...
data original;
input loanno deb cred id$;
lines;
1234 27199 0 R
1234 27199 0 R
1234 0 27199 P
1234 0 27199 P
567 2000 0 R
567 2000 0 R
567 0 2000 P
;
run;
proc sort;
by loanno deb cred id;
run;
data original;
set original;
by loanno;
if first.loanno then seq = 0;
seq+1;
run;
data even odd;
set original;
if mod(seq,2) = 0 then output even;
else output odd;
run;
proc sort data=even;
by descending loanno descending deb cred id;
run;
proc sort data=odd;
by descending loanno descending deb cred id;
run;
data final;
set odd even;
run;
proc sort;
by descending loanno;
run;
data final(drop=seq);
set final END = EOF;
if EOF then flag = "not equal";
else flag = "equal";
run;
data have;
input loanno deb cred id $;
cards;
1234 27199 0 R
1234 27199 0 R
1234 0 27199 P
1234 0 27199 P
567 2000 0 R
567 2000 0 R
567 0 2000 P
;
data temp;
set have;
by loanno id notsorted;
if first.id then n=0;
n+1;
run;
proc sort data=temp;
by loanno n;
run;
data want;
set temp;
by loanno n;
if first.n and last.n then flag='unequal';
else flag='equal ';
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.