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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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