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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 1028 views
  • 4 likes
  • 3 in conversation