Hi all I need to write code that checks that a variable names are separated correctly by a single commas & then a single space for example "DOMAIN, LBNAM, LBDAT".
So, for example, incorrect values I want to detect are:
DOMAIN , LBNAM, LBDAT
DOMAIN;LBNAM;LBDAT
DOMAIN,,LBNAM, LBDAT
DOMAIN LBNAM LBDAT
Thanks for your help
Hi @kalbo
Just check if the string is changed, i.e. s not equal to s2. The following code only outputs incorrect strings, but the comparison result could be assigned to an Error variable instead.
data a;
s = 'DOMAIN , LBNAM, LBDAT'; output;
s = 'DOMAIN;LBNAM;LBDAT'; output;
s = 'DOMAIN,,LBNAM, LBDAT'; output;
s = 'DOMAIN LBNAM LBDAT'; output;
s = 'DOMAIN, LBNAM, LBDAT'; output;
run;
data b (drop=s2); set a;
s2 = prxchange('s/(\w*)(\W*)(\w+)/$1, $3/',-1,trim(s));
if s ne s2 then output;
run;
Hi @kalbo
Try this.
data a;
s = 'DOMAIN , LBNAM, LBDAT'; output;
s = 'DOMAIN;LBNAM;LBDAT'; output;
s = 'DOMAIN,,LBNAM, LBDAT'; output;
s = 'DOMAIN LBNAM LBDAT'; output;
run;
data b; set a;
s2 = prxchange('s/(\w*)(\W*)(\w+)/$1, $3/',-1,trim(s));
run;
Hey thanks for your reply.
I tested the code and it fixes the issues. However, I wanted to detect when the issues occurred, rather than fix them. Would you be able to modify your code to do this?
thanks again.
Hi @kalbo
Just check if the string is changed, i.e. s not equal to s2. The following code only outputs incorrect strings, but the comparison result could be assigned to an Error variable instead.
data a;
s = 'DOMAIN , LBNAM, LBDAT'; output;
s = 'DOMAIN;LBNAM;LBDAT'; output;
s = 'DOMAIN,,LBNAM, LBDAT'; output;
s = 'DOMAIN LBNAM LBDAT'; output;
s = 'DOMAIN, LBNAM, LBDAT'; output;
run;
data b (drop=s2); set a;
s2 = prxchange('s/(\w*)(\W*)(\w+)/$1, $3/',-1,trim(s));
if s ne s2 then output;
run;
ok great! thanks!
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.