BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Quentin
Super User

As Tom wrote, you should try writing the data step code first, and get that to work, before writing a macro.  Remember that the macro is just generating data step code.  The macro language does not know about data step variables.

 

If you code:

%Macro AVTLF_Num(ID=);

%If %Sysfunc(lengthn(tabno))=0 %Then 
%Do;
  %Put ERROR:AVTLF_Num:TABNO variable value missing;
%End;

%Mend;

data _null_; 
  tabno='' ;
  %AVTLF_Num()
run ;

The %IF statement is always false.  The macro language does not know that TABNO is the name of a dataset variable that has a value.  %sysfunc(lengthn(tabno)) will always be 5 because the string tabno has 5 characters.

 

If you code:

%Macro AVTLF_Num(ID=);

If lengthn(tabno)=0 Then
Do;
  Put "ERROR:AVTLF_Num:TABNO variable value missing.";
End;

%Mend;

data _null_; 
  tabno='' ;
  %AVTLF_Num()
run ;

The macro will generate a DATA step IF statement (not a macro language %IF statement).  The DATA step IF statement uses the DATA step LENGTHN function of compute the length of the value of the DATA step variable TABNO. 

 

It looks to me like many of your macro %IF statements should actually be data step IF statements.

 

Most of this macro looks like assertions to be, where you are checking values.  I wrote a paper about assertions which has an %assert() macro.  https://www.lexjansen.com/nesug/nesug12/cc/cc31.pdf  It should allow you to do stuff like (untested):

 

data _null_ ;
 set have ;
 %assert(lengthn(tabno) > 0, msg="TABNO variable value missing")
 %assert(prxchange('s/[A-Z 0-9 .]//i',-1,tabno)='', msg="TABNO variable has Invalid characters")
run ;

The macro generates an IF statement to check the assertion, and if the assertion is false, it generates an error message.

 

 

 

 

 

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
Quentin
Super User

There are many problems in that macro, as others have shown.  But if you start a new SAS session, and compile that macro, it compiles correctly, without generating any error messages.  It could be you have other problems in your code (mismatch quotation marks, etc.)  before you try to compile this macro.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 16 replies
  • 3233 views
  • 2 likes
  • 5 in conversation