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 ?
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;
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;
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
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.