IF Statement

Reply
Frequent Contributor
Posts: 138

IF Statement

Hi,

I need a statement to show

if balance is a minus number then 0.

How is this written please?

Esteemed Advisor
Esteemed Advisor
Posts: 7,248

Re: IF Statement

Hi,

data want;    

     set have;

     if value < 0 then result=0;

     else result=1;

run;

result is 0 for minus numbers, 1 otherwise.

Frequent Contributor
Posts: 138

Re: IF Statement

Hi RW9,

Sorry, can i have this in proc sql please?

Super Contributor
Posts: 305

Re: IF Statement

Hello,

PROC SQL;

select *,

case when age-13 lt 0 then 0

else age-13

end as age_diff from sashelp.class;

quit;

Esteemed Advisor
Esteemed Advisor
Posts: 7,248

Re: IF Statement

Replace if /then with case/when

case     when <condition> then <result> else <default option> end as variable

Can also be

case     <variable>     when <condition> then <result> else <default option> end

E.g.

case when variable < 0 then 0 else 1 as result

Grand Advisor
Posts: 10,226

Re: IF Statement

Another approach is to not change the value just how it displays with a custom format:

proc format;

value myneg

low - 0 = 0

other = [best8.]

;

And in any display use that format by adding

Format var myneg. ;

Valued Guide
Posts: 763

Re: IF Statement

Hi ... another way in SQL ...

data old;

input balance @@;

datalines;

-1 20 56 -99 . 80 100 -0.123

;

proc sql;

select *, balance - (balance lt 0) * balance as new from old;

quit;

balance       new

---------------------------------

      -1         0

      20        20

      56        56

     -99         0

       .         .

      80        80

     100       100

  -0.123         0

though if it's just display that's important, the suggestion about using a FORMAT is probably better

Ask a Question
Discussion stats
  • 6 replies
  • 288 views
  • 0 likes
  • 5 in conversation