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

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

1 ACCEPTED SOLUTION

Accepted Solutions
ErikLund_Jensen
Rhodochrosite | Level 12

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;

View solution in original post

4 REPLIES 4
ErikLund_Jensen
Rhodochrosite | Level 12

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;
kalbo
Obsidian | Level 7

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.

ErikLund_Jensen
Rhodochrosite | Level 12

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;

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
  • 4 replies
  • 756 views
  • 0 likes
  • 2 in conversation