DATA Step, Macro, Functions and more

Round all numeric values in dataset to 3 decimal places

Accepted Solution Solved
Reply
Regular Contributor
Posts: 212
Accepted Solution

Round all numeric values in dataset to 3 decimal places

Title seems straight forward.  I'd like to round every number in a dataset to 3 decimal places, to make reading easier.

 

I'd prefer a generic approach, as I don't have variable names beforehand (using output from Proc Means, autoname).

 

Additionally, a related side question:

 

The following generic format attempt doesn't work:

 

data want;
format _num_ 9.3;
set have;
run;

SAS wants the specific variable name, not the generic _num_.

 

Any suggestions greatly appreciated.

 

Nicholas Kormanik

 

 


Accepted Solutions
Solution
‎09-16-2016 12:15 AM
Super User
Posts: 6,938

Re: Round all numeric values in dataset to 3 decimal places

[ Edited ]

If you want to actually round the values that are stored, you can use array logic:

data want;
set have;
array _nums {*} _numeric_;
do i = 1 to dim(_nums);
  _nums{i} = round(_nums{i},.001);
end;
drop i;
run;

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
SAS Super FREQ
Posts: 683

Re: Round all numeric values in dataset to 3 decimal places

[ Edited ]

hi

 

Change your FORMAT statement like so

data want;

set sashelp.class;
format _numeric_ 9.3;
run;

Please note this does not round values, it just changes how the value ist displayed.

 

Do you actually want to round the values?

 

Bruno

Solution
‎09-16-2016 12:15 AM
Super User
Posts: 6,938

Re: Round all numeric values in dataset to 3 decimal places

[ Edited ]

If you want to actually round the values that are stored, you can use array logic:

data want;
set have;
array _nums {*} _numeric_;
do i = 1 to dim(_nums);
  _nums{i} = round(_nums{i},.001);
end;
drop i;
run;

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 212

Re: Round all numeric values in dataset to 3 decimal places

[ Edited ]

Nice going Kurt.  And thanks for helping RW9, Reeza and Bruno.

 

Mission accomplished!

 

(at least for today....)

 

 

Super User
Super User
Posts: 7,401

Re: Round all numeric values in dataset to 3 decimal places

AS @Bruno_SAS has said, format does not change the underlying value, only how it is displayed.  To round a value you would use round(), floor(), ceil() functions.  You can do this based on an array if you like:

data have;
  input value1 value2;
  array tmp _numeric_;
  do over tmp;
    tmp=round(tmp,0.001);
  end;
datalines;
1.2354 34.45
23.65788 67.23456
21.34 21.34
;
run;
Super User
Posts: 17,829

Re: Round all numeric values in dataset to 3 decimal places

Things wrong with your code

1. _num_ instead of _numeric_

2. Format before SET

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 6684 views
  • 6 likes
  • 5 in conversation