BookmarkSubscribeRSS Feed

Hi,

I need a statement to show

if balance is a minus number then 0.

How is this written please?

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

data want;    

     set have;

     if value < 0 then result=0;

     else result=1;

run;

result is 0 for minus numbers, 1 otherwise.

Hi RW9,

Sorry, can i have this in proc sql please?

Loko
Barite | Level 11

Hello,

PROC SQL;

select *,

case when age-13 lt 0 then 0

else age-13

end as age_diff from sashelp.class;

quit;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

ballardw
Super User

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. ;

MikeZdeb
Rhodochrosite | Level 12

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 6 replies
  • 1107 views
  • 0 likes
  • 5 in conversation