BookmarkSubscribeRSS Feed
lokeshbm
Calcite | Level 5

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.

4 REPLIES 4
ad123123
Fluorite | Level 6

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.

 

lokeshbm
Calcite | Level 5

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"

 

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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?

Reeza
Super User

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

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
  • 4 replies
  • 1156 views
  • 0 likes
  • 4 in conversation