If now I have a data like
obs x
1 1
2 0
3 0
4 1
5 0
Variable x is numeric. The format of x is 2.
if I want to convert 1 to 1.5, the output is like
obs x
1 2
2 0
3 0
4 2
5 0
Because SAS rounded 1.5 to 2 due to the format.
If I add a format statement like format x 1.1;
Then the output is like
obs x
1 1.5
2 0.0
3 0.0
4 1.5
5 0.0
My question is that how do I change the format to get a output like
obs x
1 1.5
2 0
3 0
4 1.5
5 0
and x is still a numeric value.
Thanks.
data a;
format val 2.1;
val = 1.5;
run;
data a;
format val 3.1;
val = 1.5;
run;
run above code. for example in 3.1 format 3 belongs to total length and 1 belong to number of decimals. so the format can display 1.5 accurately, with format 2.1 the total total length 2 is smaller than your value hence it rounds up
With numeric SAS formats and if you don't require something specific like thousand separators then normally not applying any format (which then uses default BEST32.) or a format of 32. returns normally what you want.
Only use the decimal bit in a format if you need the displayed values rounded to a certain precision and you want always to display the decimals, i.e. for 2 decimals after the comma 32.2 would do the job.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.