SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Reading substrings by means of SCAN or alternative

Reply
Frequent Contributor
Posts: 89

Reading substrings by means of SCAN or alternative

Imagine that you have a folder with the following reports:

Commissions GW 0413 Ohio

Commissions GW 0413 Indiana

Commissions GW 0413 Minnesota

Commissions GW 0413 Illinois

Commissions GW 0413 Pennsylvania

Commissions GW 0413 Colorado

You want SAS to read the file names starting from "GW", and you want to turn the spaces into underscores. So, when read into SAS, the strings should look like this:

        reportname

GW_0413_Ohio

GW_0413_Indiana

GW_0413_Minnesota

GW_0413_Illinois

GW_0413_Pennsylvania

GW_0413_Colorado

I think this can be done by means of the SCAN function, but I can't seem to make it work. I'd appreciate any advice.

Thanks for your time. Smiley Happy

Super User
Posts: 5,256

Re: Reading substrings by means of SCAN or alternative

A combination of call scan/indexw and translate should do the trick.

Data never sleeps
Super Contributor
Posts: 543

Re: Reading substrings by means of SCAN or alternative

Hi.

Maybe this could work:

data want;

    set have;

    new_string = translate(trim(prxchange("s/.*Commissions //", -1, string)), "_", "");

run;

prxchange : will replace/change everything up to (including) the word Commissions with a blank space;

trim : will remove the trailing blanks (so you don't attach an underscore at the end of the string;

translate : will replace any blanks with underscore.

Good luck!

Anca.

Frequent Contributor
Posts: 89

Re: Reading substrings by means of SCAN or alternative

Thanks, I was able to do it using substr and translate. Smiley Happy

Ask a Question
Discussion stats
  • 3 replies
  • 364 views
  • 6 likes
  • 3 in conversation