In one numeric variable I have integers and floating values , I want in new variable (character) integers as it is and floating values with 2 decimal places . Please see the below table and help me ?
data have;
datalines avg;
cards;
1
5.9090888
6.3
58
8.1
;
run;
I want like below in character variable as
x( Character variable )
1
5.91
6.30
58
8.10
Thanks in advance
And a different approach:
data have;
infile cards;
input avg;
cards;
1
5.9090888
6.3
58
8.1
;
run;
data want ;
set have ;
/* Convert avg to character value and format */
cAvg=putn(avg,"12.2") ;
/* 12.2 format will force .00 which we now remove with a Perl Reg Expression */
removeDotZeros=prxchange('s/\.00//',-1,cAvg) ;
/* Output all 3 values */
put avg= cAvg= removeDotZeros=;
run ;
Hi @Minku
A simple solution is the following.
data have;
input avg;
datalines;
1
5.9090888
6.3
58
8.1
;
run;
data want; set have;
length x $8;
if avg = int(avg) then x = left(put(avg,8.0));
else x = left(put(avg,10.2));
run;
And a different approach:
data have;
infile cards;
input avg;
cards;
1
5.9090888
6.3
58
8.1
;
run;
data want ;
set have ;
/* Convert avg to character value and format */
cAvg=putn(avg,"12.2") ;
/* 12.2 format will force .00 which we now remove with a Perl Reg Expression */
removeDotZeros=prxchange('s/\.00//',-1,cAvg) ;
/* Output all 3 values */
put avg= cAvg= removeDotZeros=;
run ;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.