DATA Step, Macro, Functions and more

compress few strings in a large string

Reply
Occasional Contributor
Posts: 7

compress few strings in a large string

Hi,

 

Is there a function or a loop which can compress multiple strings in one large string.

for ex: if (salary >= 0 & salary <= 25000) then salcat_1 = "C"; 

output should look like : salary >= 0 & salary <= 25000 salcat_1 = "C"

 

Appreciate your help on this.

Occasional Contributor
Posts: 18

Re: compress few strings in a large string

Hi,

 

Could you please explain criterian for removing multiple strings?

 

 if (salary >= 0 & salary <= 25000) then salcat_1 = "C"; 

 

In the string above , you are eliminating "if" and "then". Will this be the same always? Every time we need to eliminate only "If" and "else"? Please be more explicit.

 

Occasional Contributor
Posts: 7

Re: compress few strings in a large string

I am importing a file which has few strings {ex: IF THEN ELSE SELECT etc} ,I now wanted to remove those strings{not pre post text,only the above mentioned words} from the dataset .

As i have split those in to observations,I am in serach of a best approach/logic, Just posted a basic example below.

 

 input : if (salary >= 0 & salary <= 25000) then salcat_1 = "C"; 

Output : salary >= 0 & salary <= 25000 salcat_1 = "C"

 

 

 

Super User
Super User
Posts: 7,966

Re: compress few strings in a large string

What about the ()?  What about other characters?  If you just want to remove those then tranwrd() or compress() can do it:

data want;
  set have;
  string=tranwrd(string,"if","");
  string=tranwrd(string,"then","");
run;

However I question what you are doing, you appear to be reading in code, which can make parsing more difficult than just removing some characters, e.g.

if (abc="I go on holiday if I booked it) then result="Sometimes (always)";

will result in:

(abc="I go on holiday I booked it) result="Sometimes (always)";

 

Personally, I would look at other methods to do this, including looking at the documentation - you do have that?

Super User
Posts: 19,815

Re: compress few strings in a large string


lokeshbm wrote:

I now wanted to remove those strings{not pre post text,only the above mentioned words} from the dataset .

 

 


 

I can't see any list of mentioned words or characters. You need to define those. 

Then you can use a combination of translate or transwrd functions. 

Regular expressions can probably help as well.

Ask a Question
Discussion stats
  • 4 replies
  • 267 views
  • 0 likes
  • 4 in conversation