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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

7 REPLIES 7
LinusH
Tourmaline | Level 20

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

Data never sleeps
UrvishShah
Fluorite | Level 6

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

Ksharp
Super User

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

Haikuo
Onyx | Level 15

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

suraestar
Calcite | Level 5

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..

Xianhua_zeng
Fluorite | Level 6

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;

suraestar
Calcite | Level 5

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.

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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