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

Hi Guys,

I am Shriram from Bangalore, India. I am new to this community/forum. I have just about started learning SAS and have lots of doubts.

I would be glad and grateful if anyone can give me pointers on how to approach the subject, how to ask mny questions and any other suggestions.

 

Heres is to a start of our association:

 

Scenario 1: No problems and it works fine

data _null_;

x=123.45567;

y=round(x,0.1); /* nth decimal place*/

z=round(x,100); /* nearest 100 place*/

m=round(x,10);  /*nearest 10 place*/

put y;

put z;

put m;

run;

 

Scenario 2: how to round of columns of an incoming data set

data roundtest;  

set <libref>.filename;

then how to write the code for rounding of columns y w r l and k. (Attached screen shot of columns)

the data comprises of columns y w r l and K and year. Now all these columns(except year) are in 8-9th decimal place. I want to round of to the first decimal  place?...


Incoming data file_to be rounded down.PNGIncoming data file_to be rounded down.PNG
1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You seem to have already answered your own question there:

data roundtest;  

  set <libref>.filename;

  y=round(y,0.1);

  w=round(w,0.1);

  ...;

run;

View solution in original post

10 REPLIES 10
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You seem to have already answered your own question there:

data roundtest;  

  set <libref>.filename;

  y=round(y,0.1);

  w=round(w,0.1);

  ...;

run;

bgs
Obsidian | Level 7 bgs
Obsidian | Level 7

Thank you @RW9

 

Appreciate your help. Expected something more complicated.

 

LOL. Starting troubles 🙂

 

Astounding
PROC Star

To me, the most important piece of advice is not to round unless you absolutely have to.  You can control how many decimal places to use when printing by applying a format.  For example:

 

data test;

x=123.4567;

y=x;

z=x;

run;

 

proc print data=test;

format y 8.1;

format z 8.2;

run;

 

You can let variables keep their actual value without rounding, but still print rounded versions.

 

Good luck.

bgs
Obsidian | Level 7 bgs
Obsidian | Level 7

@Astounding - Thank you. Yes I agree with you.

 

 

Please guide me on this:

You have used 8.1 and 8.2 - this is new to me, as in I was not aware there were number formats like 8.1...

 

How do i learn short cuts like that - as in can you share a link or something or am i approaching this the right way?

Hope my question makes sense!

 

🙂

bgs
Obsidian | Level 7 bgs
Obsidian | Level 7

@Astounding or if i were to rephrase my earlier question

 

Can you also explain (lead to to the explanation link) how this works?

 

As in if i use format 1.1 or 2.1 or ......8.1.... etc

What is the difference or what is the logis used by sas to apply these formats onto the dataset?

ballardw
Super User

The "logic" for applying formats depends on what you are doing. Many analysis procedures will have a default to display a given number of decimals but you can direct the output to a data set and print the results with a different number of decimals, or you may be able to override the default. Here is an example:

proc tabulate data=sashelp.class;
   class sex;
   var height weight;
   table sex, (height weight)*(n mean std)
          /box='Default formats for statistics';
   table sex, (height weight)*(n mean*f=f8.4 std*f=best12.)
          /box='Specified formats for statistics';
run;

You can also use formats to group data

 

Proc freq data=sashelp.class;
   tables weight;
run;

proc format library=work;
   value WeightGroup
   0 - 85   = ' 0 <= 85'
   85< - 110= '85 <=110'
   110< - high='110>';
run;

Proc freq data=sashelp.class;
   tables weight;
   format weight WeightGroup.;
run;

 

bgs
Obsidian | Level 7 bgs
Obsidian | Level 7
Thank you @ballardw.
the second code I Understood. First one I have to disect and try out and then ill reach out to you.
Astounding
PROC Star

There are dozens of numeric formats available within the software. 

 

It isn't a question of explaining a few tricks.  Formatting is one of the basics.  Just start somewhere, and add to what you know over time.

 

To explain 8.1 and 8.2, here are a few general rules.  The first number ("8" in this case) refers to the total number of positions being used to print a numerical value.  That includes all digits (whether before or after the decimal point), the decimal point, and (if needed) a negative sign.  If you are using other numeric formats, such as those that print a dollar sign or a comma, the first number still includes every character being printed.  The second number ("1" or "2" in this case) is the number of digits to print after the decimal point.  SAS will automatically round as needed.

 

If you apply a format when reporting, it does not change the data.  It only changes the form in which that report prints the data.

 

Hope this helps!

bgs
Obsidian | Level 7 bgs
Obsidian | Level 7
ok that makes sense. Drawing on the same - explanation iv read/understood from u and elsewhere is that numbers are stored in 8 bytes of data hence the 8 and then 1.2.... after the decimal for round off accordingly.
Astounding
PROC Star

Truthfully, it's just coincidence that numbers are stored in 8 bytes.  SAS stores the value (not the digits themselves) in a totally different form (binary floating point).  SAS can accurately store an integer with 15 to 16 significant digits (depends on the operating system).  Very similar explanations would apply to other numeric formats (whether longer or shorter):  comma14.2, dollar5.0, for example.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 10 replies
  • 2098 views
  • 4 likes
  • 4 in conversation