BookmarkSubscribeRSS Feed
VAMA
Calcite | Level 5
Hi,
I am confused with behaviour of %LENGTH function, here is example to explain
%let line=%nrbquote(a;b;c);
%let line_length=%length(%sysfunc(tranwrd(&line,%str(a),%str(k))));

Since %sysfunc unquotes result I was expecting %length function would give error but it did not. Can someone explain why I am not getting any error?
7 REPLIES 7
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
The SAS macro compiler was not invoked with your "open" SAS code statements. Wrap your code within a macro and the error occurs because of the imbedded semi-colon in the first %LET for macro variable LINE.

Scott Barry
SBBWorks, Inc.
VAMA
Calcite | Level 5
Thanks yes you are correct however I do not full understand: %sysfunc and %length are executed in macro execution and not on macro comiplation, so why does open code make difference?
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
The SAS support http://support.sas.com/ website's SEARCH facility can turn you on to much about the macro variable and macro variables.

Also, recommend starting here...

SAS Macro Language: Introduction to the Macro Facility
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a002293969.htm


Scott Barry
SBBWorks, Inc.
VAMA
Calcite | Level 5
I have already read these documents ..when you replied back i was qick to copy open code in macro and it gave error - however i realized it was giving error because I forgot to put semi-colons in %str i.e.

%macro temp;
%let line=%nrbquote(a%str(;)b%str(;)c);
%let line_length=%length(%sysfunc(tranwrd(&line,%str(a),%str(k))));
%mend temp;
%temp;
Now it will not give error.
however I expect an error because %sysfunc(.. will unquote result whiich actually containts semi colon. if you replace
%let line_length=%length(%sysfunc(tranwrd(&line,%str(a),%str(k))));
by %let line_length=%sysfunc(tranwrd(&line,%str(a),%str(k)));
it will give error because unquoting is occuring.
So I am not clear what does %length do so that there is no error?
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Suggest you add the line below to generate more diagnostics during compilation. The problem is that your semi-colon characters have meaning while the macro compiler is resolving &line but prior to the outer functions.

options source source2 macrogen symbolgen mlogic nomprint;

Hopefully with this additional SAS log diag output, you will see what's going on with the macro compiler as it is parsing and resolving each code line.

Also, suggest using additional statements (as needed for diagnosis), such as:

%put _user_;

%put _all_;

%put _global_;


Good luck with your experience.

Scott Barry
SBBWorks, Inc.
VAMA
Calcite | Level 5
Ok I get your point seems like I have refer to macro documentation and update my understanding 🙂
data_null__
Jade | Level 19
Maybe for nested function calls as used here the "value" that %LENGTH operates on is not passed back to the word scanner but is passed to %LENGTH as a pointer.

Isn't it the word scanner that needs simicolons and other special symbols quoted?

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
  • 897 views
  • 0 likes
  • 3 in conversation