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!!
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
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
Hi Tom,
Thanks . Best4.1 works for me coz i don't expect any value with more then 1(or max two decimal places). 🙂
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.