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). 🙂
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.