DATA Step, Macro, Functions and more

How to use a macro variable in index function?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 75
Accepted Solution

How to use a macro variable in index function?

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,


Accepted Solutions
Solution
‎07-24-2014 02:14 PM
Trusted Advisor
Posts: 3,212

Re: How to use a macro variable in index function?

Posted in reply to Shayan2012

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


All Replies
Trusted Advisor
Posts: 1,228

Re: How to use a macro variable in index function?

Posted in reply to Shayan2012

%let str=William;

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

Solution
‎07-24-2014 02:14 PM
Trusted Advisor
Posts: 3,212

Re: How to use a macro variable in index function?

Posted in reply to Shayan2012

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 --<-----
Frequent Contributor
Posts: 75

Re: How to use a macro variable in index function?

Posted in reply to Shayan2012

Thanks a lot guys!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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