The SAS Output Delivery System and reporting techniques

Taking substring from the name of the variable macro

Accepted Solution Solved
Reply
Contributor
Posts: 46
Accepted Solution

Taking substring from the name of the variable macro

Hi all,

I'd like to now if is it possible take a substr from the variable macro's name.

Something like that:

%let Test = 0

Now, I'd like to do a substr from the name "Test", Is it possible ?

Tk's in advanced!


Accepted Solutions
Solution
‎01-24-2013 10:28 AM
Trusted Advisor
Posts: 1,128

Re: Taking substring from the name of the variable macro

Hi,

you can use the substr function to extract the part of the string. however when you use the sas function in macro variable we should use %sysfunc(). like below

%let test=%str(good day);

%put &test;

%let first=%sysfunc(substr(&test,1,4));

%put &first;

However for your question as per the post you should change the code this way

%let test=0;

%put &test;

%let first=%sysfunc(substr(&test,1,1));

%put &first;

Thanks,

Jagadish

Thanks,
Jag

View solution in original post


All Replies
Solution
‎01-24-2013 10:28 AM
Trusted Advisor
Posts: 1,128

Re: Taking substring from the name of the variable macro

Hi,

you can use the substr function to extract the part of the string. however when you use the sas function in macro variable we should use %sysfunc(). like below

%let test=%str(good day);

%put &test;

%let first=%sysfunc(substr(&test,1,4));

%put &first;

However for your question as per the post you should change the code this way

%let test=0;

%put &test;

%let first=%sysfunc(substr(&test,1,1));

%put &first;

Thanks,

Jagadish

Thanks,
Jag
Super User
Posts: 17,784

Re: Taking substring from the name of the variable macro

You can substring the variable name, but you'd need to know it before hand.

%let test=0;

%put &test;

%let first=%sysfunc(substr(test,1,1));

%put &first;

If you're looking to substring the value of your macro variable then Jagadish's solution will work.

Regular Contributor
Posts: 198

Re: Taking substring from the name of the variable macro

Yes, the macro language has a %substr function.

%Let Mvar = value;

%Let part = %substr(&Mvar,1,2);

%put note: part: &Part.;

note: part: va

The way I read your Q you want to get a substring of a macro variable name;

that does not compute. What could you do with that info?

Ron Fehd  macro maven

Respected Advisor
Posts: 3,124

Re: Taking substring from the name of the variable macro

Hi, This could be completely off-topic without knowing your purpose. But SAS stores all of the Macro variable name in : dictionary.macros (only for sql) or sashelp.vmacro. From there you can just treat them as regular text strings and apply many powerful parsing functions.

Haikuo

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 2804 views
  • 9 likes
  • 5 in conversation