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?...
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;
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;
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.
@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!
🙂
@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?
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;
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!
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.