How to compress data value

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

How to compress data value

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?


Accepted Solutions
Solution
‎12-09-2014 04:32 AM
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: How to compress data value

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


All Replies
Esteemed Advisor
Posts: 5,198

Re: How to compress data value

Use findw and substr functions.

Data never sleeps
Occasional Contributor
Posts: 13

Re: How to compress data value

Could you please suggest sample syntax?

Thanks,

Abhee

Esteemed Advisor
Posts: 5,198

Re: How to compress data value

Support.sas.com

Data never sleeps
Respected Advisor
Posts: 3,825

Re: How to compress data value

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;

Occasional Contributor
Posts: 13

Re: How to compress data value

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

Respected Advisor
Posts: 3,825

Re: How to compress data value

So it worked then.

Occasional Contributor
Posts: 13

Re: How to compress data value

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.

Solution
‎12-09-2014 04:32 AM
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: How to compress data value

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;

Occasional Contributor
Posts: 13

Re: How to compress data value

Thanks RW9. It worked.

SAS Employee
Posts: 340

Re: How to compress data value

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".

Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: How to compress data value

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;

SAS Employee
Posts: 340

Re: How to compress data value

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;

New Contributor Vix
New Contributor
Posts: 4

Re: How to compress data value

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


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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 13 replies
  • 334 views
  • 0 likes
  • 6 in conversation