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