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 (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
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 (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 16 replies
  • 3048 views
  • 2 likes
  • 5 in conversation