BookmarkSubscribeRSS Feed
ramuking003
Calcite | Level 5

CODE:
%macro ss(s=);

data d;
a=%substr("&s",2,%length(&s)-3)dy;
run;

%mend;

%ss(s=cmstdtc)

 

OUTPUT:

a=cmstdy

 

If im giving the substr position as 1 its not working but, if i give the position as 2 its working correctly. i want to know how this code works.Plz check the answer for a in log using "options mprint symbolgen".

 

Thanks in advance.

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

Please describe what you are trying to accomplish with your code? 🙂

Quentin
Super User

The macro language does not use quotation marks to indicate a text value.  In the macro language, all values are text, so they are not needed.  If you use quote marks they become part of the value.

 

Below shows the quote marks are part of the value:

37   %let s=cmstdtc;
38   %put The length of macro var S is: %length(&s);
The length of macro var S is: 7
39   %put The length of macro var S with quote marks is: %length("&s");
The length of macro var S with quote marks is: 9

 

It's a macro best practice to avoid adding quote marks to your values.  If you choose to add quote marks, you need to remember that they are there, and account for them, which is why starting at position two works:

40   %put %substr("&s",2,%length(&s)-3)dy;
cmstdy

If you started at position 1, the quote mark would be part of the value returned by %substr()  [and you would end up with unmatched quoation marks].

 

But if you avoid adding the quote marks, you can start at position one, as you would expect:

 

41   %put %substr(&s,1,%length(&s)-3)dy;
cmstdy

 

The quote marks are needed in the SAS language to indicate that a literal value is character rather than numeric.  In the macro language, all values are character, so quotation marks are not used to to indicate character values.

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
ramuking003
Calcite | Level 5

Now i understood what mistake i have done thank you. 🙂

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

 

I am in agreement with @PeterClemmensen here, there seems to be no point to this code, other than to obfuscate code?  Substr is a datastep function, not a macro function - so nothing like %susbtr().  Create your question using the guidance provided.  Provide test data - in the form of a datastep, and examplpe of what the output should look like.

BenbowL
Fluorite | Level 6

Hi ramuking,

 

The reason you are experiencing the error when you change the "start" portionof the %substr to 1 if the quotation marks you have used on the "source" portion of the %substr.

 

Macro language, unlike dataset, is purely text.  So there is no need for you to use quotations in your %substr function, as text is all it will read.

 

So when you are including the quotation marks the result you are getting from the %substr is "cmsdy, making your datastep read:

data d;

a = "cmsdy;

run;

and this is why it does not work.

 

In contrast, when you used 2 as your "start" your %substr function return cmstdy, making your datastep read:

data d;

a = cmstdy;

run;

as you desired.

 

So long story short, don't include the quotation marks in the source portion of your %substr function.

 

Hope this helps! 🙂

ramuking003
Calcite | Level 5

Thank you. 🙂

Loko
Barite | Level 11

Hello,

 

You are messing things. You try applying macro functions in order to set data variables.

 

Macro functions shall be used to manipulate macro variables.

 

Just answering your question - the code works in terms of no error message but if you look at the database created, the values

are missing therefore in fact the code does not work.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 7 replies
  • 2319 views
  • 0 likes
  • 6 in conversation