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

Hello experts,

I would like to remove extra comma in different locations of the text.  Sometimes, front, back, and in the middle.   In addition, there might be a double at the front or in the end.   Is there a way to do that?  Thank you.

 

data Have;
      infile datalines delimiter='#';
  input Comments : $200.  ;
datalines;
	, , FAILURE TO THRIVE #
	CHOLESTASIS, ,  ANEMIA#
	UGESTAGE=33.4 WKS , ROP #
	ASD, , #
	, NON VERBAL, 
;
run;

data want;
      infile datalines delimiter='#';
  input Comments : $200.  ;
datalines;
	FAILURE TO THRIVE #
	CHOLESTASIS, ANEMIA#
	UGESTAGE=33.4 WKS , ROP #
	ASD#
	NON VERBAL 
;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data Have;
      infile datalines delimiter='#';
  input Comments : $200.  ;
datalines;
	, , FAILURE TO THRIVE #
	CHOLESTASIS, ,  ANEMIA#
	UGESTAGE=33.4 WKS , ROP #
	ASD, , #
	, NON VERBAL, 
;
run;

data want;
 set have;
 temp=prxchange('s/,\s*,/,/',-1,Comments);
 want=prxchange('s/^\s*,\s*|\s*,\s*$//',-1,temp);
 drop temp;
run;

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

Always comma-space-comma? Use the TRANWRD function.

 

Comma at the end: Use the STRIP() function to remove leading and trailing blanks, then find the last character and remove it using

IF SUBSTR(REVERSE(string),1,1)=',' THEN STRING=SUBSTR(STRING,1,LENGTH(STRING)-1);

 

Comma at the beginning: obvious modification of the code to remove comma at the end

 

--
Paige Miller
Ksharp
Super User
data Have;
      infile datalines delimiter='#';
  input Comments : $200.  ;
datalines;
	, , FAILURE TO THRIVE #
	CHOLESTASIS, ,  ANEMIA#
	UGESTAGE=33.4 WKS , ROP #
	ASD, , #
	, NON VERBAL, 
;
run;

data want;
 set have;
 temp=prxchange('s/,\s*,/,/',-1,Comments);
 want=prxchange('s/^\s*,\s*|\s*,\s*$//',-1,temp);
 drop temp;
run;
ybz12003
Rhodochrosite | Level 12
Thank you so much!
PaigeMiller
Diamond | Level 26

@Ksharp 

 

One of these days, I need to learn regular expressions. You have quite a mastery of them!

 

Which kind of brings up a question: I am pretty good at parsing strings using built in SAS functions such as SUBSTR() and ANYDIGIT() and dozens of other ones., what is the benefit of regular expressions over and above what SAS provides?

--
Paige Miller
Ksharp
Super User
In some case, only regular expressions can get job done, therefore PRX was a must-have skill for sas programmer as much as Hash Table .
PaigeMiller
Diamond | Level 26

Thanks, @Ksharp , regular expressions are definitely on my list of things to learn, along with 27 bazillion other things (some of them having nothing to do with SAS).

--
Paige Miller
Ksharp
Super User
Are you learning R , Python ?
ybz12003
Rhodochrosite | Level 12
Neither of them.
ybz12003
Rhodochrosite | Level 12
7 year ago was my first time seeing PERI. After 7 year, I still have no clue how to use it. LOL!!!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 9 replies
  • 2104 views
  • 6 likes
  • 3 in conversation