BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
havmaage
Obsidian | Level 7

I have this MACRO variable that i get from a PROMPT in a SAS EG project. 

 

I want to validate the string by using regex to match it, 

 

my code 

Im only creation dataset a for debugging, normally it would be _null_

DATA a;
a= prxmatch("/^TBDLZL\d{4}_[A-Z]/","&tablename"); 
IF prxmatch("/^TBDLZL\d{4}_[A-Z]/","&tablename") GT 0 THEN DO;

%PUT "THE TABLE IS MATCHED OK";

END;
ELSE DO;
%put ERROR: table name &tablename does not match;
stop; 

END;

RUN;

I behaves funny, the A variable in dataset a get the value 1 indicating that there is a match, but the conditionnel test dont get the same result, it test it to be zero 

the value of &tablename is  TBDLZL2223_AALLE_SDDS

 

is it my condition that is wrong ? 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You are using %put, which is a macro statement, and executed immediately when the code is compiled.

As you can see in the log:

29         DATA a;
30         a= prxmatch("/^TBDLZL\d{4}_[A-Z]/","&tablename");
31         IF prxmatch("/^TBDLZL\d{4}_[A-Z]/","&tablename") GT 0 THEN DO;
32         
33         %PUT "THE TABLE IS MATCHED OK";
"THE TABLE IS MATCHED OK"
34         
35         END;
36         ELSE DO;
37         %put ERROR: table name &tablename does not match;
ERROR: table name TBDLZL2223_AALLE_SDDS does not match
38         stop;
39         
40         END;
41         
42         RUN;

Both %put's are executed before the following statements are even read.

 

Use the data ste statement put instead:

data a;
a = prxmatch("/^TBDLZL\d{4}_[A-Z]/","&tablename"); 
if prxmatch("/^TBDLZL\d{4}_[A-Z]/","&tablename") gt 0
then put "THE TABLE IS MATCHED OK";
else put "ERROR: table name &tablename does not match";
run;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

You are using %put, which is a macro statement, and executed immediately when the code is compiled.

As you can see in the log:

29         DATA a;
30         a= prxmatch("/^TBDLZL\d{4}_[A-Z]/","&tablename");
31         IF prxmatch("/^TBDLZL\d{4}_[A-Z]/","&tablename") GT 0 THEN DO;
32         
33         %PUT "THE TABLE IS MATCHED OK";
"THE TABLE IS MATCHED OK"
34         
35         END;
36         ELSE DO;
37         %put ERROR: table name &tablename does not match;
ERROR: table name TBDLZL2223_AALLE_SDDS does not match
38         stop;
39         
40         END;
41         
42         RUN;

Both %put's are executed before the following statements are even read.

 

Use the data ste statement put instead:

data a;
a = prxmatch("/^TBDLZL\d{4}_[A-Z]/","&tablename"); 
if prxmatch("/^TBDLZL\d{4}_[A-Z]/","&tablename") gt 0
then put "THE TABLE IS MATCHED OK";
else put "ERROR: table name &tablename does not match";
run;
PeterClemmensen
Tourmaline | Level 20

I made a small correction to you RegEX, which I think is necessary 

 

a= prxmatch("/^tbdlzl\d{4}_[a-z_]*/","&tablename"); 

Also, use the Data Step Put Statement instead of the Macro %Put 

havmaage
Obsidian | Level 7
Thanks, yes your right this match better
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
  • 3 replies
  • 1031 views
  • 3 likes
  • 3 in conversation