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). 🙂
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.