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;
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.
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.
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.
Thanks a lot, Reeza.
That works perfectly. But, for my understanding, could you please let me know why my code does not work properly?
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.
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.