BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Shayan2012
Quartz | Level 8

Hi all,

So, I am trying to search for some strings in a text, and I want to use the strings with macro variable so I be able to change the string. However, I am not sure how to use the macro variable in the index function.

For example,I am doing such thing:

%let str=William;

y=index(line,&str);

Is this the correct form since there are no ' ' around the variable &str? I am asking this because I see the SAS does not find the specified string in the text, although I know it is there.

I will be grateful if someone helps me.

Thanks a lot,

1 ACCEPTED SOLUTION

Accepted Solutions
jakarman
Barite | Level 11

When you are working with text-string in an SAS-macro environment look at the sas-macrofunctions (they all start with a %)

When you are using SAS-macro variables in a sas-datastep  and process those in a string make sure there quotes.   '

The macro-variable itself can contain  quotes

%let str='William';

y=index(line,&str);

or use the double quotes to indicate the & are sasmacro-variables to be resolved. (Single quotes will cause no resolving of macros)

%let str=William;

y=index(line,"&str");

---->-- ja karman --<-----

View solution in original post

3 REPLIES 3
stat_sas
Ammonite | Level 13

%let str=William;

y=index(line,"&str");

jakarman
Barite | Level 11

When you are working with text-string in an SAS-macro environment look at the sas-macrofunctions (they all start with a %)

When you are using SAS-macro variables in a sas-datastep  and process those in a string make sure there quotes.   '

The macro-variable itself can contain  quotes

%let str='William';

y=index(line,&str);

or use the double quotes to indicate the & are sasmacro-variables to be resolved. (Single quotes will cause no resolving of macros)

%let str=William;

y=index(line,"&str");

---->-- ja karman --<-----
Shayan2012
Quartz | Level 8

Thanks a lot guys!

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 5393 views
  • 5 likes
  • 3 in conversation