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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 2 replies
  • 980 views
  • 0 likes
  • 3 in conversation