SAS Macro %substr

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

SAS Macro %substr

Dear all,

I try to keep only the first 11 character from string with carriage return.

ex: 

"In progress. # 1 DCF[ CLOSED ] 02/03/2017 15:46Mr A. ADJE wrote null.and proposed value 999,999,999. 30/06/2017 11:10Mme 
M. CANEAU wrote ok.and proposed value null. # 1 Notification. Change performed by Mme M. CANEAU on 30/06/2017 11:10 Smiley Surprisedk-new 
value: null"

 

 Please find my sentence in my macro:

%let fileDel2=%unquote(%sysfunc(getvarc(&tabid2,%sysfunc(varnum(&tabid2,&nomvar2))))); 
%put &fileDel2; 
%let nou=%substr(&fileDel2,1,11);
%put &nou;

 

The log displays:
In progress. # 1 DCF[ CLOSED ] 02/03/2017 15:46Mr A. ADJE wrote null.and proposed value 999,999,999. 30/06/2017 11:10Mme 
M. CANEAU wrote ok.and proposed value null. # 1 Notification. Change performed by Mme M. CANEAU on 30/06/2017 11:10 : ok - new 
value: null
ERROR: Macro function %SUBSTR has too many arguments.
ERROR: The macro KEEPMONITORTAB will stop executing.

 

If i perform directly on table without macro, it works.

What can I do for that works also throught macro?

 

Thanks for your help


Accepted Solutions
Solution
2 weeks ago
Super User
Posts: 5,081

Re: SAS Macro %substr

You can probably (can't get it right now) get away with this:

 

%let nou = %unquote( %substr(%superq(fileDel2), 1, 11) ) ;

 

Most likely, the problems occur because of the commas within the value of &fileDel2.  %SUBSTR interprets them as marking the end of one parameter and the beginning of the next.

View solution in original post


All Replies
Super User
Posts: 6,936

Re: SAS Macro %substr

Simple answer: don't do it. Macro language is for the creation of dynamic code, not for manipulating data.

An abomination like

%let fileDel2=%unquote(%sysfunc(getvarc(&tabid2,%sysfunc(varnum(&tabid2,&nomvar2)))));

is the consequence of trying to abuse the macro processor.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
2 weeks ago
Super User
Posts: 5,081

Re: SAS Macro %substr

You can probably (can't get it right now) get away with this:

 

%let nou = %unquote( %substr(%superq(fileDel2), 1, 11) ) ;

 

Most likely, the problems occur because of the commas within the value of &fileDel2.  %SUBSTR interprets them as marking the end of one parameter and the beginning of the next.

Contributor
Posts: 26

Re: SAS Macro %substr

Thank  you that works

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 87 views
  • 0 likes
  • 3 in conversation