DATA Step, Macro, Functions and more

Using Let Statement in a Do Loop

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 75
Accepted Solution

Using Let Statement in a Do Loop

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;


Accepted Solutions
Solution
‎05-07-2015 01:07 PM
Super User
Posts: 17,868

Re: Using Let Statement in a Do Loop

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


All Replies
Solution
‎05-07-2015 01:07 PM
Super User
Posts: 17,868

Re: Using Let Statement in a Do Loop

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.

Trusted Advisor
Posts: 1,621

Re: Using Let Statement in a Do Loop

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.

Frequent Contributor
Posts: 75

Re: Using Let Statement in a Do Loop

Thanks a lot, Reeza.

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

Super User
Posts: 17,868

Re: Using Let Statement in a Do Loop

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.

Super User
Posts: 9,682

Re: Using Let Statement in a Do Loop

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;

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 688 views
  • 3 likes
  • 4 in conversation