DATA Step, Macro, Functions and more

%length with quoted data

Reply
Occasional Contributor
Posts: 8

%length with quoted data

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?
Super Contributor
Super Contributor
Posts: 3,174

Re: %length with quoted data

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.
Occasional Contributor
Posts: 8

Re: %length with quoted data

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?
Super Contributor
Super Contributor
Posts: 3,174

Re: %length with quoted data

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.
Occasional Contributor
Posts: 8

Re: %length with quoted data

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(Smiley Winkb%str(Smiley Winkc);
%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?
Super Contributor
Super Contributor
Posts: 3,174

Re: %length with quoted data

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.
Occasional Contributor
Posts: 8

Re: %length with quoted data

Ok I get your point seems like I have refer to macro documentation and update my understanding Smiley Happy
Respected Advisor
Posts: 3,777

Re: %length with quoted data

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?
Ask a Question
Discussion stats
  • 7 replies
  • 153 views
  • 0 likes
  • 3 in conversation