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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.