SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Cornelis
Fluorite | Level 6

Dear all,

 

I have a rounding issue and here is the simplified code:

 

data a;
format c $5.;
b= '1.04';
c = left(round(b, 0.1));
run;

 

The output: c= 1.

 

Cornelis_0-1643267204876.png

However, the desired output must be 1.0 not 1

It is possible to apply different SAS function as solution to add '.0' to 1 if it is too sharply rounded, but I wonder whether is an easy solution.

 

Thanks and best regards,

 

Cornelis

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

how about

 

data _null_;
   b = '1.04';
   c = put(round(input(b, 8.2), .1), 8.1 -l);
   put b = / c = ;
run;

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

how about

 

data _null_;
   b = '1.04';
   c = put(round(input(b, 8.2), .1), 8.1 -l);
   put b = / c = ;
run;
Cornelis
Fluorite | Level 6

Hi Peter,

 

This is a reasonable good solution, the put statement in combination with format can be quickly applied in the existing programming code.

 

Thank you,

 

Cornelis

Kurt_Bremser
Super User

The real question here is: why are numbers used for calculations stored in a character variable, which makes handling them unnecessarily hard?

Keep in mind that computers should make your life easier with every step, not harder.

Cornelis
Fluorite | Level 6

Dear Kurt,

 

The situation is less easy.

I received a large set of data from a database system and the column contains the next value in character:

0.12

0.6

93.6

93.0

11200

25.1

 

The values (characters) in a column of a table are all correctly rounded because it depends on the level of analytical instrument.

One instrument rounds with no digit, the other with 1 or 2. That is therefore the reason why I get dataset with character format.

 

Best regards,

 

Kurt_Bremser
Super User

Since you want to discard information (everything after the first decimal), these numbers seem to be real numeric values and not some type of category. In this case, you are better off omitting the re-conversion to character and store the data in a numeric variable with a format that has one digit after the decimal dot.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 5 replies
  • 1779 views
  • 4 likes
  • 3 in conversation