DATA Step, Macro, Functions and more

Format on decimal numbers

Accepted Solution Solved
Reply
Contributor
Posts: 43
Accepted Solution

Format on decimal numbers

Hi All,

I have a column in dataset which has numeric values. I am converting these numeric values to character using put function.

Like a= put (b,4.);

Now this numeric variable, b can have one decimla place too, like 9.1, 8.2 etc,.

When I apply the format a= put(b,$4.1), SAS appends the decimal distd to even nondecomal mnumbers like

when b = 5 , 6 then a is character 5.0 and 6.0

Now my problem is, I dont want .0 when there is no decimal place int he number.

How can I do that without writing a big code.

Thanks!!


Accepted Solutions
Solution
‎07-31-2012 01:43 PM
Super User
Super User
Posts: 7,074

Re: Format on decimal numbers

Posted in reply to maggi2410

Best is to use TRANWRD to remove the .0. You could try using BEST4.1 format, but it has trouble with numbers that do not fit into decimal fractions (like 1/3).  You could put in a ROUND function call.  What do want to do with 1.999999999 or 1.00000001 ?

285  data _null_;

286    do x=1,3.4,1+1/3;

287      y=put(x,4.1);

288      y2=tranwrd(y,'.0',' ');

289      z=put(x,best4.1);

290      z2=put(round(x,0.1),best4.1);

291      put (_all_) (=);

292

293    end;

294  run;

x=1 y=1.0 y2=1 z=1 z2=1

x=3.4 y=3.4 y2=3.4 z=3.4 z2=3.4

x=1.3333333333 y=1.3 y2=1.3 z=1.33 z2=1.3

View solution in original post


All Replies
Solution
‎07-31-2012 01:43 PM
Super User
Super User
Posts: 7,074

Re: Format on decimal numbers

Posted in reply to maggi2410

Best is to use TRANWRD to remove the .0. You could try using BEST4.1 format, but it has trouble with numbers that do not fit into decimal fractions (like 1/3).  You could put in a ROUND function call.  What do want to do with 1.999999999 or 1.00000001 ?

285  data _null_;

286    do x=1,3.4,1+1/3;

287      y=put(x,4.1);

288      y2=tranwrd(y,'.0',' ');

289      z=put(x,best4.1);

290      z2=put(round(x,0.1),best4.1);

291      put (_all_) (=);

292

293    end;

294  run;

x=1 y=1.0 y2=1 z=1 z2=1

x=3.4 y=3.4 y2=3.4 z=3.4 z2=3.4

x=1.3333333333 y=1.3 y2=1.3 z=1.33 z2=1.3

Contributor
Posts: 43

Re: Format on decimal numbers

Posted in reply to maggi2410

Hi Tom,

Thanks . Best4.1 works for me coz i don't expect any value with more then 1(or max two decimal places). :-)

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 1117 views
  • 0 likes
  • 2 in conversation