As others have shown, the second parameter in INDEX can refer to a variable. It doesn't have to be a fixed string. However, when you use a variable, there is one issue that you might need to attend to: the possibility of trailing blanks. If you want the variable V1 to be the second parameter (the string to be searched for), it would be safer to use strip(v1) as the second parameter.
I did a small test though i am not sure what you are after:
Using index, indexw, find and findw
data have;
V1="sgd";
v2="hdsgdk";
output;
V1="kjh";
v2="ukjhso";
output;
run;
data want;
set have;
index=index(v2,v1);
indexw=indexw(v2,v1);
find=find(v2,v1);
findw=findw(v2,v1);
run;
Unless I'm not understanding the question properly you've answered it yourself ![]()
data _null_;
v1="sgd";
v2="hdsgdk";
if index(v2,v1) then put "found";
else put "not found";
v1="kjh";
v2="ukjhso";
if index(v2,v1) then put "found";
else put "not found";
/* Just for contrast */
v1="sgd";
v2="ukjhso";
if index(v2,v1) then put "found";
else put "not found";
run;
As others have shown, the second parameter in INDEX can refer to a variable. It doesn't have to be a fixed string. However, when you use a variable, there is one issue that you might need to attend to: the possibility of trailing blanks. If you want the variable V1 to be the second parameter (the string to be searched for), it would be safer to use strip(v1) as the second parameter.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.