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

Hi,

 

I want to remove the string ' ,' from the column Code.

 

ID                      CODE

1

,ABD

2

,VBG

3

,NMJ,KJH,LKJ

4

,TYU,IOU,DEF

5

,TYY

6

,JKK

7

,LKJ

8

,MKL

Output Should be as below:

1

ABD

2

VBG

3

NMJ,KJH,LKJ

4

TYU,IOU,DEF

5

TYY

6

JKK

7

LKJ

8

MKL

 

Thanks for your help !

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15
substr(code,2)

If you always have ',' as the first char.

 

View solution in original post

6 REPLIES 6
Haikuo
Onyx | Level 15
substr(code,2)

If you always have ',' as the first char.

 

ballardw
Super User

If it is only a leading , that you do not want then in a data step.

 

if code =: ',' then code = substr(code,2);

 

If ALL of your values have the leading , then you only need:

code = substr(code,2);

cho16
Obsidian | Level 7

Thank You Ballardw !

 

Can you please let me know how to remove the ',' value at the front and also remove the comma at the end.

 

 

ID                      CODE

1

,ABD,

2

,VBG,

3

,NMJ,KJH,LKJ,

4

,TYU,IOU,DEF,

5

,TYY,

6

,JKK,

7

,LKJ,

8

,MKL

 

Output:

 

ID                      CODE

1

,ABD

2

,VBG

3

,NMJ,KJH,LKJ

4

,TYU,IOU,DEF

5

,TYY

6

,JKK

7

,LKJ

8

,MKL

ballardw
Super User

Please describe your data and what you actually need in a bit more detail. If you keep adding rules then the original solution may not be valid.

Do ALL of your values start with a comma?

Do ALL of your values end with a comma?

If the answer to either of these is not yes then you need to process the data conditionally.

 

to test for a comma at the end that needs to be removed:

if substr(code,length(code))=',' then code=substr(code,1,length(code)-1);

 

If there was a leading comma to remove the above line of code would be AFTER the part that removed the leading comma.

 

If you need to always remove both a leading and trailing comma:

code = substr(code,2,length(code)-2);

should work.

Ksharp
Super User
data comma;
input ID code$ 15.;
cards;
1    ,ABD,
2    ,VBG,
3    ,NMJ,KJH,LKJ,
4    ,TYU,IOU,DEF,
5    ,TYY,
6    ,JKK,
7    ,LKJ,
8    ,MKL
;
run;

data want;
 set comma;
 want=prxchange('s/^,|,$//',-1,strip(code));
run;

soham_sas
Quartz | Level 8

data comma;
input ID code$ 15.;
cards;
1    ,ABD,
2    ,VBG,
3    ,NMJ,KJH,LKJ,
4    ,TYU,IOU,DEF,
5    ,TYY,
6    ,JKK,
7    ,LKJ,
8    ,MKL

;
proc print;
run;

/*to reome the leading the leading comma;*/

data remove_leading_comma;
set comma;
N_code = substr(code,2);
run;

/*to remove all the comma*/

data remove all_comma;
set comma;
N_code = TRANSLATE(code ,"",',');
run;


/*to reomove the trailing comma*/

data remove_trailing_comma;
set comma;
N_code = substr(code,1,length(code)-2);
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
  • 6 replies
  • 15675 views
  • 2 likes
  • 5 in conversation