## Extract numerical data from a field

Solved
Occasional Contributor
Posts: 14

# Extract numerical data from a field

I get multiple file with the format below.

i need to extract only the number from the files

"TRAILER|407"

I am using a scan function For this.

x=scan(variable,2,'|');

When I try to convert variable x into a numerical variable

y=input(x,8.);

y does not contain any value. But if I give y=input(x,3.) it works.

when I do a length(x) , I am getting the length as 4 bytes. I need to get rid of the fourth byte and only extract the numerical part .

i Used compress(x,' .') but that does not remove the fourth byte.

Accepted Solutions
Solution
‎04-17-2014 08:18 PM
Super Contributor
Posts: 644

## Re: Extract numerical data from a field

Try

x=compress(variable, ,'KD');

this should remove all non numeric characters (including non printing ones) from the original variable.

KD => Keep Decimal

Better

y=input(compress(variable, ,'KD'), 8.);

The length of the informat does not need to be 3 if the vaue of x only contains numerals.

Richard

All Replies
Super User
Posts: 13,542

## Re: Extract numerical data from a field

What value does x actually have? And if you are reading external files you might be able to solve this issue with infile and input options.

On the long shot that your variable = TRAILER|407" means that " are part of the variable then add the " to the scan

x=scan(variable,2,'|"'); /* which is hard to see that there is a single " in the delimiter list for scan.

Occasional Contributor
Posts: 14

## Re: Extract numerical data from a field

when I see the output data set , only 407 appears in the data set

the variable is just  TRAILER|407

Super User
Posts: 23,724

## Re: Extract numerical data from a field

Why do you need a 3 byte number, 1 byte doesn't correspond to a digit?

A 4 byte number contains number going up to 2,000,000.

Step-by-Step Programming with Base SAS(R) Software

Super User
Posts: 13,542

## Re: Extract numerical data from a field

If you get a visible value of 407 and a length of 4 you likely have something else after the 7.

What do you get if you do a

put x \$hex.;

If x is 407 with no trailing character you should get 343037 in the log. If there is a trailing space, which shouldn't affect input, you would get 34303720.

Solution
‎04-17-2014 08:18 PM
Super Contributor
Posts: 644

## Re: Extract numerical data from a field

Try

x=compress(variable, ,'KD');

this should remove all non numeric characters (including non printing ones) from the original variable.

KD => Keep Decimal

Better

y=input(compress(variable, ,'KD'), 8.);

The length of the informat does not need to be 3 if the vaue of x only contains numerals.

Richard

Occasional Contributor
Posts: 14

## Re: Extract numerical data from a field

Thanks it works

🔒 This topic is solved and locked.

Discussion stats
• 6 replies
• 248 views
• 0 likes
• 4 in conversation