BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kmardinian
Quartz | Level 8

Hi,

 

I have this line of code that is calculating the mean of a variable. For a couple variables, the mean is a very high negative number (-0.00001) and because I only want one decimal place, SAS ends up printing the mean as -0.0. Is there a way to get rid of this negative sign

 

This is the code that I am using:

 

data statall (keep=trt_dec row1 row2 row3 row4 vid);
merge sesstat(in=a) median;
by vid trt_dec;
if a;
row1=strip(put(n,8.0));
if mean ne . then row2=strip(put(mean, 8.1))||' ('||strip(put(std, 8.2))||')';
if mean =. then row2='-';
if median ne . then row3=strip(put(round(median, .1), 8.1))||' ('||strip(put(min, 8.0))||', '||strip(put(max, 8.0))||')';
if median =. then row3='-';
row4='('||strip(put(uclm, 8.1))||', '||strip(put(lclm, 8.1))||')';
run;

 

Thank you, any help is appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
Anotherdream
Quartz | Level 8

Put converts your numeric data into character data.  your code SHOULD already be returning a result of 0.0, I am not sure why it isn't so I think something else is going on.

MY suggestion was to keep the data as numeric and simply use an actual FORMAT Answer 8.1;

 

What do you get when you run the following code?  The second observation should return 0.0, are you seeing only 0?

 

data ShoWUser;
infile datalines delimiter=',';
input Number
;
datalines;
15.514
-0.00001
9523.51
3960.26
;

data yay;
set ShoWUser;
answer=strip(put(round(number,.1),8.1));
run;

View solution in original post

10 REPLIES 10
PeterClemmensen
Tourmaline | Level 20

Just to be sure, you want the value -0.00001 to be displayed as 0.0, correct?

kmardinian
Quartz | Level 8

Yes, exactly! Just 0.0

 

Thank you!

ruchitpatel
Calcite | Level 5

Is there a method to display -0.00001 as -0.0? Rounding method is removing the negative sign. Want to see a method which retains the negative sign. 

kmardinian
Quartz | Level 8

I tried that, but it then gets rid of my decimal place and I am just left with '0'

Anotherdream
Quartz | Level 8

So do you just want a format to the number to be decimal X.1?

 

So like...

 

format answer 17.1;

 

(17 needs to be big enough to fit your largest integer obviously i just used it as an example)....


So as other users have noticed round your number using round(VAL,.1)  and then simply format the new number you came up with?

Kurt_Bremser
Super User

In addition to rounding, use a proper display format with one decimal.


@kmardinian wrote:

I tried that, but it then gets rid of my decimal place and I am just left with '0'


 

kmardinian
Quartz | Level 8

Can you give me an example of a proper display format?  Is that not the 8.1 format I used in my code above after the mean variable?

Anotherdream
Quartz | Level 8

Put converts your numeric data into character data.  your code SHOULD already be returning a result of 0.0, I am not sure why it isn't so I think something else is going on.

MY suggestion was to keep the data as numeric and simply use an actual FORMAT Answer 8.1;

 

What do you get when you run the following code?  The second observation should return 0.0, are you seeing only 0?

 

data ShoWUser;
infile datalines delimiter=',';
input Number
;
datalines;
15.514
-0.00001
9523.51
3960.26
;

data yay;
set ShoWUser;
answer=strip(put(round(number,.1),8.1));
run;

kmardinian
Quartz | Level 8

Thank you so much, that worked so well!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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