BookmarkSubscribeRSS Feed
Alucard_L
Calcite | Level 5

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.

2 REPLIES 2
kiranv_
Rhodochrosite | Level 12

 

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

Patrick
Opal | Level 21

@Alucard_L

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.

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1233 views
  • 0 likes
  • 3 in conversation