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

Hi All,

So, I am doing some text processing and cleaning, and have got this little issue.  I want to omit the words 'director' or 'officer' (and a lot more words) from the variable name.

For this, I create the do loop, which goes through the list of words and omits them from the variable.

what I want to add, is to save the omitted word in another variable, so I know what was the omitted one. For example, if the name is GARY OFFICER, then the varibale name_h would be just GARY, and variable O would be OFFICER. This is why I define the let statement in the do loop, so that it saves the list.

The code, however, is not working correctly, and I am thinking that probably my use of let statement is not correct.

Can someone please help me on that?

Thanks.

here's the code:

DATA have;

   INPUT name $1-30;

   DATALINES;

GARRY OFFICER

PATRICK DIRECTOR

JOHN

;

run;

data want (drop = list);

  set have;

  name_h = name;

  do list = 'DIRECTOR','OFFICER';

  % let i = list;

  if index(strip(name_h),list) > 0 then O = &i;

  name_h = tranwrd(strip(name_h),strip(list),'');

  end;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

I'd recommend using a temporary array and looping over that instead:

data want (drop = list);

  set have;

array search(2) $ _temporary_ ('DIRECTOR', 'OFFICER');

  name_h = name;

  do i=1 to dim(search);

  list=search(i);

  if index(strip(name_h),strip(list)) > 0 then O = list;

  name_h = tranwrd(strip(name_h),strip(list),'');

  end;

run;

If you have your list i.e. Director/Officer in a dataset there are ways to load that into a temporary array using a data step.

View solution in original post

5 REPLIES 5
Reeza
Super User

I'd recommend using a temporary array and looping over that instead:

data want (drop = list);

  set have;

array search(2) $ _temporary_ ('DIRECTOR', 'OFFICER');

  name_h = name;

  do i=1 to dim(search);

  list=search(i);

  if index(strip(name_h),strip(list)) > 0 then O = list;

  name_h = tranwrd(strip(name_h),strip(list),'');

  end;

run;

If you have your list i.e. Director/Officer in a dataset there are ways to load that into a temporary array using a data step.

PaigeMiller
Diamond | Level 26

Adding to the above answer ...

There's no need to complicate what you are doing with a macro variables here, data step variables work fine.

--
Paige Miller
Shayan2012
Quartz | Level 8

Thanks a lot, Reeza.

That works perfectly. But, for my understanding, could you please let me know why my code does not work properly?

Reeza
Super User

1. Macro variables in a data step are generally created using call symput, not %let. You can use %let if you use macro loops and other logic but this is too much work.

2. Macro variables created in a data step are generally not available in the same data step. You can get around this in various ways using resolve() function but it's too much work in my opinion.

3. Your assignment to O works out to be a variable assignment - O=Director when you actually need quotation marks - O="&i" assuming the other logic was correct.

Ksharp
Super User

DATA have;

   INPUT name $1-30;

   DATALINES;

GARRY OFFICER

PATRICK DIRECTOR

JOHN

;

run;

data want;

set have;

g=prxchange('s/OFFICER|DIRECTOR//io',-1,name);

if prxmatch('/OFFICER|DIRECTOR/',name) then o=prxchange('s/.*(OFFICER|DIRECTOR).*/$1/io',-1,name);

run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 2474 views
  • 3 likes
  • 4 in conversation