## Scan Function

Solved
Frequent Contributor
Posts: 110

# Scan Function

Hi All,

I have a data field that I need to split into two new fields. The field is a building code and then name of that building. Example: ID_Number= 1 Bond Street Building

I've been using this code to extract the ID_number:

do while(scan(ID_Number, i, " "));

new_id=scan(LID_Number, i, "  ");

output;

i+1;

end;

That code gives me a new_id field of 1. How do I get "Bond Street Building" into another field?

Thanks,

Sarah

Accepted Solutions
Solution
‎06-18-2013 10:41 AM
Super User
Posts: 6,785

## Re: Scan Function

One possibility:

field1 = scan(id_number, 1, ' ');

field2 = substr(id_number, 2 + length(field1));

Scan is good for getting the first field, but not as good at getting the second field because that one contains multiple words.

Good luck.

All Replies
Super User
Posts: 5,884

## Re: Scan Function

There have been plenty of questions of this sort on the forum. Pls use the search facility to get your answer.

Data never sleeps
Super User
Posts: 23,774

## Re: Scan Function

Look at the scan function modifiers, specifically 'ka'

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

Frequent Contributor
Posts: 110

## Re: Scan Function

Thanks Reeza, I'm unable to get that to work either. Is there a second step I'm missing? I've tried the following and the field NewName is not populated.

NewName=compress(ID_Number,,'ka');

NewName=scan(LDMS_ID_Number,2," ","ka");

NewName=scan(ID_Number," ","ka");

NewName = PRXCHANGE('s/[^a-zA-Z]//o',-1,ID_Number);

Solution
‎06-18-2013 10:41 AM
Super User
Posts: 6,785

## Re: Scan Function

One possibility:

field1 = scan(id_number, 1, ' ');

field2 = substr(id_number, 2 + length(field1));

Scan is good for getting the first field, but not as good at getting the second field because that one contains multiple words.

Good luck.

Frequent Contributor
Posts: 110