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

Hello, I am trying to convert a character variable to a numeric variable. I used the INPUT function in the advanced expression builder, but the data that is currently a character field has commas and parenthesis in it. My guess is that I need to remove any special characters prior to converting the data to numeric. 

 

I can't change the field on import because I have to transpose the data to get it in the correct order. 

 

INPUT(t1.'Total Pre-Tax Book Income:'n,BEST12.)

 

Any help would be great! I am a very new user to this program. 

 

Brooke

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Try using the COMMA informat instead.

Otherwise use COMPRESS() function to remove the characters you don't want.

input(compress(xxx,'.','kd'),32.)

Examples:

data test;
  input char $32.;
  num1=input(char,??32.);
  num2=input(char,??comma32.);
  num3=input(compress(char,'.','kd'),??32.);
cards;
123
1.3
$123
123,456
(123,456)
;
Obs    char          num1         num2        num3

 1     123          123.0        123.0       123.0
 2     1.3            1.3          1.3         1.3
 3     $123            .         123.0       123.0
 4     123,456         .      123456.0    123456.0
 5     (123,456)       .     -123456.0    123456.0

View solution in original post

4 REPLIES 4
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

I find that if I create a dummy like this I am able to convert the numbers into character

data have(rename=(myvar2=myvar));

set have

myvar2 = input(myvar, best12.);

drop myvar;

run;

 

 

'

Tom
Super User Tom
Super User

Try using the COMMA informat instead.

Otherwise use COMPRESS() function to remove the characters you don't want.

input(compress(xxx,'.','kd'),32.)

Examples:

data test;
  input char $32.;
  num1=input(char,??32.);
  num2=input(char,??comma32.);
  num3=input(compress(char,'.','kd'),??32.);
cards;
123
1.3
$123
123,456
(123,456)
;
Obs    char          num1         num2        num3

 1     123          123.0        123.0       123.0
 2     1.3            1.3          1.3         1.3
 3     $123            .         123.0       123.0
 4     123,456         .      123456.0    123456.0
 5     (123,456)       .     -123456.0    123456.0
bmenter
Calcite | Level 5

Hello, thank you for your solution, can you tell me what I am doing wrong? I lose the negative sign for negative values? Thank you!! I attached a screen shot of the results. 

 


PROC SQL;
CREATE TABLE WORK.OTP_PBT_AND_TAX_GRP AS
SELECT t1.Source,
t1.'Total Total Pre-Tax Book Income'n,
t1.'Total Tax Provision'n,
/* PTBI */
(INPUT(COMPRESS(t1.'Total Total Pre-Tax Book Income'n, ' . ' , 'kd'),BEST32.)) FORMAT=BEST32. AS PTBI,
/* PTBI1 */
(INPUT(t1.'Total Total Pre-Tax Book Income'n, BESTcomma32.)) AS PTBI1
FROM WORK.TRNSTRANSPOSED_0000 t1;
QUIT;

Tom
Super User Tom
Super User

Include hyphen along with period in the list of characters to keep.

But if you want to read the parentheses as meaning negative value then use the COMMA informat.

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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