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

I need to compress one of the value in the character string.

e..g, if value of string is "mango,orange,papaya" and I want to create a new variable with value "orange,papaya". I used below code:

     new_var = compress(string,"mango");

     and the result was new_var = ,re,ppy

Please suggest how can I do that?

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Try a do while loop over the data split by commas and then append each one where not your value to a new variable:

data have;
  val="mango,orange,papaya";
  output;
run;

data want (drop=i);
  set have;
  length processed $2000.;
  i=1;
  do while (scan(val,i,",") ne "");
    if scan(val,i,",") ne "mango" then processed=catx(',',processed,scan(val,i,","));
    i=i+1;
  end; 
run;

View solution in original post

13 REPLIES 13
LinusH
Tourmaline | Level 20

Use findw and substr functions.

Data never sleeps
abhee
Calcite | Level 5

Could you please suggest sample syntax?

Thanks,

Abhee

LinusH
Tourmaline | Level 20

Support.sas.com

Data never sleeps
Patrick
Opal | Level 21

Compress() will remove from the source string ALL characters listed in the compress function.

If this is not just a study question then you will have to be a bit more specific and provide some sample data with expected results for us to come up with an appropriate solution.

Below code will replace the string "papaya" with a blank.

data test;

  have="mango,orange,papaya";

  want = tranwrd(have,"papaya","");

run;

abhee
Calcite | Level 5

Thanks for your prompt response. However I want to remove mango. I used below code:

data test;

  have="mango,orange,papaya";

  want = tranwrd(have,"mango","");

run;

and the result was: want = ,,orange,papaya

Patrick
Opal | Level 21

So it worked then.

abhee
Calcite | Level 5

No, it did not. I am getting comma also as I have mentioned earlier.

e.g.,      ,,orange,papaya

I want: orange,papaya

Since I am working on confidential data, I am sorry I will not be able to share the sample data.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Try a do while loop over the data split by commas and then append each one where not your value to a new variable:

data have;
  val="mango,orange,papaya";
  output;
run;

data want (drop=i);
  set have;
  length processed $2000.;
  i=1;
  do while (scan(val,i,",") ne "");
    if scan(val,i,",") ne "mango" then processed=catx(',',processed,scan(val,i,","));
    i=i+1;
  end; 
run;

abhee
Calcite | Level 5

Thanks RW9. It worked.

gergely_batho
SAS Employee

Just add a comma (,) to the end of mango.

data test;

  have="mango,orange,papaya";

  want = tranwrd(have,"mango,","");

  putlog want=;

run;

But if you want to handle situations, where mango can be at the beginning, end, and in the middle of the string... Maybe you need to use tranwrd 2 times. Once with "mango," then with ",mango".

RW9
Diamond | Level 26 RW9
Diamond | Level 26

The only thing I would add there Gergely is that you might end up with lots of tranwrds() nestled if you have more than one thing to remove, also you could run into problems trying to remove orange from "blood orange" for instance.  You could of course expand it with arrays and loops:

data have;
  val="mango,orange,papaya,fruit,veg,apple,kiwi";
  output;
run;

data want (drop=i j x removes1-removes4);
  set have;
  array removes{4} $20. ("mango","fruit","veg","orange");
  length processed $2000.;
  i=1;
  x=0;
  do while (scan(val,i,",") ne "");
    do j=1 to 4;
      if scan(val,i,",")=removes{j} then x=1;
    end;
    if x=0 then processed=catx(',',processed,scan(val,i,","));
    i=i+1;
    x=0;
  end; 
run;

gergely_batho
SAS Employee

I would use regexp, if I have to remove more then one thing Smiley Happy

data have;

  val="papaya,mango,mango,orange,papaya,fruit,veg,apple,kiwi,orange,orange,papaya,papaya";

  output;

run;

data want (keep=processed);

  array removes{4} $20. ("mango","fruit","veg","orange");

  length processed $2000;

  length tmp       $200 ;

/*constructing the regexp*/

  if _n_=1 then do;

  do i=1 to dim(removes);

  tmp=catx('|',tmp,'(,?'||strip(removes)||')');

  end;

putlog tmp=;

  regexp=cats('s/',tmp,'//');

putlog regexp=;

  regexpid=prxparse(regexp);

  end;

  set have;

  processed=prxchange(regexpid,-1,val);

  processed=prxchange('s/(^,)//',-1,processed);/*removing ',' from beginning*/

putlog processed=;

run;

Vix
Calcite | Level 5 Vix
Calcite | Level 5

data have;
  val="mango,orange,papaya";
  output;
run;


data new;
set have;
val1= cats("'",substr(val,(find(val, ',')+1)),"'");
run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 13 replies
  • 1219 views
  • 0 likes
  • 6 in conversation