Help using Base SAS procedures

IF THEN Debugging

Reply
Contributor
Posts: 35

IF THEN Debugging

Hi Guys,

Need some help with this code:

DATA Rawcompustat8;

SET work.Rawcompustat6;

IF PSTKRV>0 THEN BM=(SEQ-PSTKRV+TXDITC);

IF PSTKRV<0 and PSTKL>0 THEN BM=(SEQ-PSTKL+TXDITC);

IF PSTKRV<0 and PSTKL<0 and PSTK>0 THEN BM=(SEQ-PSTK+TXDITC);

IF PSTKRV<0 and PSTKL<0 and PSTK<0 THEN BM=(SEQ+TXDITC);

RUN;

This code works fine for the first 3 sentences of "IF". However, the last "IF" gave outputs of periods (.)

I can't figure out where I went wrong.

Thanks guys!

Respected Advisor
Posts: 3,887

Re: IF THEN Debugging

I've re-coded your IF sequence a bit. It still does exactly the same - I just find it easier to debug.

data rawcompustat8 missed_cases;
   set work.rawcompustat6;

   if pstkrv>0 then bm=(seq-pstkrv+txditc);
   else if pstkrv<0 then
   do;
     if pstkl>0 then bm=(seq-pstkl+txditc);
     else if pstkl<0 then
     do;
       if pstk>0 then bm=(seq-pstk+txditc);
       else if pstk<0 then bm=(seq+txditc);
       else output missed_cases;
     end;
   end;

   output rawcompustat8;
run;

I can see 2 reasons for ending up with missings.

1. You don't cover cases where your "selection" variables are exactly zero - so there "BM" will be missing

2. For the case where "IF PSTKRV<0 and PSTKL<0 and PSTK<0" is true one or both of the 2 variables you're summing is missing - so the result stored in BM would be missing (which is represented as a '.').

I've added a table for missed cases. In there will be all rows you've missed which should allow you to determine why your code is not working as expected.

Ask a Question
Discussion stats
  • 1 reply
  • 132 views
  • 0 likes
  • 2 in conversation