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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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

4 REPLIES 4
Jagadishkatam
Amethyst | Level 16

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
Reeza
Super User

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.

Ron_MacroMaven
Lapis Lazuli | Level 10

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

Haikuo
Onyx | Level 15

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

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
  • 4 replies
  • 9241 views
  • 9 likes
  • 5 in conversation