Format question

Reply
Occasional Contributor
Posts: 5

Format question

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.

PROC Star
Posts: 514

Re: Format question

[ Edited ]
Posted in reply to Alucard_L

 

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

Respected Advisor
Posts: 4,741

Re: Format question

[ Edited ]
Posted in reply to Alucard_L

@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.

 

Ask a Question
Discussion stats
  • 2 replies
  • 207 views
  • 0 likes
  • 3 in conversation