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.
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
A combination of SUBSTR() and INDEXW() should do the trick.
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
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
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
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..
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;
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.