BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Minku
Fluorite | Level 6

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

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
AMSAS
SAS Super FREQ

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 ;

View solution in original post

4 REPLIES 4
ErikLund_Jensen
Rhodochrosite | Level 12

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;

 

AMSAS
SAS Super FREQ

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 ;
Minku
Fluorite | Level 6
Thanks
Minku
Fluorite | Level 6
Thank you 🙂

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 833 views
  • 2 likes
  • 3 in conversation