Hello,
I have following codes:
data test;
input var $;
cards;
BACABBAD
;
data test;
set test;
position=index(var,"A");
run;
here, position=2, which is the position of first "A" in the string. Now, my question is: how can I get the position of third (or second, fourth, ...) "A" in the string?
You can use the FIND() function. It will let you specify a starting position for the search. If you want to search for any single character from a list of characters then use the FINDC() function.
Example:
data test;
input var $;
put var / @ ;
position=find(var,"A");
do while (position);
put @position 'X' @ ;
position=find(var,"A",position+1);
end;
cards;
BACABBAD
;
.BACABBAD
. X X X
I would recommend you look into Help Document for the FINDC function.
I think it would be more suitable to what you want to do.
Hope this helps,
Ahmed
AhmedAl-Attar wrote:
I would recommend you look into Help Document for the FINDC function.
I think it would be more suitable to what you want to do.
Hope this helps,
Ahmed
This is a good answer but requires RTM and no one does that anymore.
Try this:
data test;
input var $;
cards;
BACABBAD
AAAAAAAA
XXXXXXXX
XXAAXXAX
;
run;
data test(keep=var msg);
set test;
length msg $ 80;
n=count(var,'A');
if n=0 then msg="Any letter 'A' found";
else do;
msg="Letter 'A' found in positions ";
var2=var;
do i=1 to n;
p=index(var2,'A');
msg=strip(msg) || put(p,2.);
substr(var2,p,1)='*';
end;
end;
run;
CTorres
Your best bet is to learn the PRX functions, such as PRXPOSN. If you had to do this the "old school" way, here is an approach to find the 3rd occurrence:
data want;
set have;
position_n=3;
string_to_search = value || ' ';
position=0;
do _n_=1 to position_n;
found_location = index(string_to_search, "A");
if found_location then do;
position + found_location;
string_to_search = substr(string_to_search, found_location + 1);
end;
else do;
position = 0;
_n_ = position_n;
end;
end;
run;
There can be (solvable) complications when the string you are searching for is a blank, rather than "A". The code is untested, so you may need to tweak it mildly.
Good luck.
DATA HAVE;
ATTRIB VAR LENGTH = $8.;
INPUT VAR $;
DATALINES;
BACABADA
AAAAAAAA
;
RUN;
%LET DIM = ;
%LET DSID = %SYSFUNC(OPEN(HAVE,I));
%LET DIM = %SYSFUNC(VARLEN(&DSID,%SYSFUNC(VARNUM(&DSID,VAR))));
%LET RC = %SYSFUNC(CLOSE(&DSID));
%PUT ********&DIM;
DATA WANT;
SET HAVE;
ATTRIB VAR1 LENGTH=$&DIM..;
ARRAY POS APOS1-APOS&DIM.;
DO I = 1 TO COUNT(VAR,"A");
IF I = 1 THEN DO;
POS{I} = INDEX(VAR,"A");
VAR1 = SUBSTR(VAR,INDEX(VAR,"A")+1);
END;
ELSE DO;
POS{I} = SUM(INDEX(VAR1,"A"),POS{I-1});
VAR1 = SUBSTR(VAR1,INDEX(VAR,"A")+1);
END;
END;
DROP VAR1;
RUN;
Thank you all. I heard about rgx in sas but never tried; I will try the rgx.
Thanks.
data test;
input var $;
cards;
BACABBAD
;
run;
data A_at;
set test;
do until (index(var,'A')=0);
position=index(var,"A");
var=substr(var,position+1);
A_at+position;
output;
end;
drop position;
run;
You can use the FIND() function. It will let you specify a starting position for the search. If you want to search for any single character from a list of characters then use the FINDC() function.
Example:
data test;
input var $;
put var / @ ;
position=find(var,"A");
do while (position);
put @position 'X' @ ;
position=find(var,"A",position+1);
end;
cards;
BACABBAD
;
.BACABBAD
. X X X
Thank you all for great help.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.