« Reusable Data Merge Macro | Main | Setting variable value based on value of another variable »

Identify commonly named variables - Multiple solutions

I love how there is always more than 1 way to solve a problem in SAS.  Here is a perfect example (replicated below).
If var starts with ("Mc" "Mac") then …

*Here’s our data and solution 1;*

data test; infile cards missover; input @1 var $30.; if var in:(’Mc’,'Mac’) then mix = 1; if upcase(var) in:(’MC’,'MAC’) then upcase = 1;
cards;
McTarnihan
Mc Ewan
Lagunator
Grolsch
Tooth Sheaf
Tadcaster
Anderson
Hop Rod Rye
Mac Beth
mackinaw
mac Donalds
;;;;
run;

*Solution 2;

data test2;
set test END=eof;
retain rx;
if _n_=1 then rx=rxparse(”` (mac|mc) [’ ‘]$c*$w*”);

match=rxmatch(rx,var);
if(match>0) then
put “Got a match, ” var= match=;
else
put “Not a match, ” var= match=;
if eof then call rxfree(rx);
run;

*Solution 3;

Data test3;
set test;
if index(var,’Mc’) = 1 or index(var,’Mac’) = 1 then match = 1;
run;

Posted on Monday, March 31, 2008 by Registered CommenterJared in | CommentsPost a Comment

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>