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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 625 views
  • 0 likes
  • 2 in conversation