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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 767 views
  • 0 likes
  • 5 in conversation