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 ;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.