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 have multiple values on the column CODES and need to delete some of them which has 101,102,103,104,1110,

 

Prov   Codes

  • 101, 102,103,104,007
  • 101, 102,103,104,1110 ,007,008
  • 108,109,1110,1111

 

Want

 

Prov   Codes

  • 007
  • 007,008
  • 1111

Thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
japelin
Rhodochrosite | Level 12

How about this code?

BTW, you have removed 108,109,1110 from 108,109,1110,1111.

Isn't it correct that it should be 108,109,1111?

 

data want;
  set have;
  length char $200;
  cnt=countw(codes,',');
  char='';
  do i=1 to cnt ;
    x=scan(codes,i);
    if x not in ('101','102','103','104','1110') then char=catx(',',char,x);
  end;
  keep char;
run;

 

 

View solution in original post

5 REPLIES 5
japelin
Rhodochrosite | Level 12

How about this code?

BTW, you have removed 108,109,1110 from 108,109,1110,1111.

Isn't it correct that it should be 108,109,1111?

 

data want;
  set have;
  length char $200;
  cnt=countw(codes,',');
  char='';
  do i=1 to cnt ;
    x=scan(codes,i);
    if x not in ('101','102','103','104','1110') then char=catx(',',char,x);
  end;
  keep char;
run;

 

 

cho16
Obsidian | Level 7

Yes Kawakami , you are right !

The code works perfect.

btw, how to include all other columns in the below code ..got error when I tried it.

andreas_lds
Jade | Level 19

Replace the keep statement with

drop cnt i codes;
japelin
Rhodochrosite | Level 12

@andreas_lds  pointed out.
Just change the keep statement to a drop statement and specify the variable that was used temporarily.

 

andreas_lds
Jade | Level 19

Alternative version, not fully tested:

data want2;
   set have;
   
   length reduced $ 100;
   
   codes = compress(codes);
   reduced = prxchange('s/(,?101)|(,?102)|(,?103)|(,?104)|(,?1110)//', -1, codes);
   reduced = substr(reduced, anydigit(reduced));
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 5 replies
  • 1955 views
  • 1 like
  • 3 in conversation