BookmarkSubscribeRSS Feed
rohithverma
Obsidian | Level 7

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

3 REPLIES 3
novinosrin
Tourmaline | Level 20


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;

   
VENKATAMAHESH
Calcite | Level 5

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;
Ksharp
Super User

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;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 3 replies
  • 934 views
  • 0 likes
  • 4 in conversation