Function to Fetch n words from a variable separated by a Delimiter?

Accepted Solution Solved
Reply
Contributor
Posts: 46
Accepted Solution

Function to Fetch n words from a variable separated by a Delimiter?

Dear All,

data _null_;

     varlist='Hello$Test$System$SAS';

     key=scan(varlist,2,'$');

     put key=;

run;

In the above code, the output would be key=Test since I have given 2 in the scan function.

But my requirement is I should get output as key=Hello$Test

If I pass 3 ie. [key=scan(varlist,3,'$');] , then my output should be key=Hello$Test$System.

Is there any function in SAS to extract the word as like SUBSTR from n no. of words specfied by a Delimiter?

Thanks
Surae.


Accepted Solutions
Solution
‎04-23-2013 09:37 AM
Respected Advisor
Posts: 3,124

Re: Function to Fetch n words from a variable separated by a Delimiter?

Of course Ksharp's solution is by far the most elegant . But If you want to taste some flavor of PRX, here is one possibility:

%let n=2;

data _null_;

  varlist = 'Hello$Test$System$SAS';

ptn=prxparse('/[^\$]+/');

start=1;stop=length(varlist);

call prxnext(ptn,start,stop,varlist,pos,len);

do i=1 to &n.-1 while (pos > 0);

  call prxnext(ptn,start,stop,varlist,pos,len);

end;

varlist_want=substr(varlist,1,pos+len-1);

put varlist_want=;

run;

Haikuo

View solution in original post


All Replies
Super User
Posts: 5,257

Re: Function to Fetch n words from a variable separated by a Delimiter?

A combination of SUBSTR() and INDEXW() should do the trick.

Data never sleeps
Regular Contributor
Posts: 195

Re: Function to Fetch n words from a variable separated by a Delimiter?

Use the SUBSTR function only to get the output...Why it is not coming by using SUBSTR functio ???

data _null_;

    varlist = 'Hello$Test$System$SAS';

    key = substr(varlist,1,10);

    put key =;

run;


-Urvish

Super User
Posts: 9,681

Re: Function to Fetch n words from a variable separated by a Delimiter?

call scan() is what you are looking for.

data _null_;
     varlist='Hello$Test$System$SAS';
     call scan(varlist,4,pos,len,'$');
      key=substr(varlist,1,pos-2);
     put key=;
run;

Ksharp

Solution
‎04-23-2013 09:37 AM
Respected Advisor
Posts: 3,124

Re: Function to Fetch n words from a variable separated by a Delimiter?

Of course Ksharp's solution is by far the most elegant . But If you want to taste some flavor of PRX, here is one possibility:

%let n=2;

data _null_;

  varlist = 'Hello$Test$System$SAS';

ptn=prxparse('/[^\$]+/');

start=1;stop=length(varlist);

call prxnext(ptn,start,stop,varlist,pos,len);

do i=1 to &n.-1 while (pos > 0);

  call prxnext(ptn,start,stop,varlist,pos,len);

end;

varlist_want=substr(varlist,1,pos+len-1);

put varlist_want=;

run;

Haikuo

Contributor
Posts: 46

Re: Function to Fetch n words from a variable separated by a Delimiter?

Dear All,

Thanks for your ideas.  and i get the expected output from Hai.Kuo..

and Ksharp code was helped in some other logic.

But is there any other way to do it more simpler.??

Surae..

Occasional Contributor
Posts: 12

Re: Function to Fetch n words from a variable separated by a Delimiter?

Hi, suraestar,

A simple method.You can try it.

data _null_;

     varlist='Hello$Test$System$SAS';

     key=prxchange('s/(.+?)(\$)(.+?)\$(.+)/$1$2$3/',-1,varlist);

     put key=;

run;

Contributor
Posts: 46

Re: Function to Fetch n words from a variable separated by a Delimiter?

Hi, Thanks,but this seems to be little pre-processing may require. If my varlist exceeds more than 5 words, then I should rebuild the prxchange parse code.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 307 views
  • 6 likes
  • 6 in conversation