BookmarkSubscribeRSS Feed
kojuramesh
Calcite | Level 5

example:

Var 1 as full_address '123 ABC STEET APT 305 MINNEAPOLIS MN 55332' and '333 5TH ST #3 SAINT PAUL 55343'

Var 2 as city 'EDINA' and 'SAINT PAUL'

 

if city exist in full_address, output the result

Below are few codes I tried didn't work. 

 

data MATCHING_CITY;
    set ADDR_DATA; 
 
 
if index(FULL_ADDRESS, CITY) > 0 then do;
output;
end;
 
run;
 
data MATCHING_CITY;
    set ADDR_DATA; 
 
if find(FULL_ADDRESS, CITY) > 0 then do;
output;
end;
run;
 
data MATCHING_CITY;
    set ADDR_DATA; 
 
/*Initialize match flag*/
 
   Match_flag = 0;
 
    /* Iterate through each city in CITY */
    do i = 1 to countw(CITY, ' ');
        home_city = scan(CITY, i, ' ');
 
 
        /* Check if home_city exists within cactus_ben_nm_addr_comb_cpr */
        if findw(FULL_ADDRESS, home_city, ' ', 'i') > 0 then do;
            /* Match found */
            Match_flag=1;
leave;
        end;
    end;
 
    drop home_city i; /* Drop temporary variables */
run;
 
before I proceed these code I already, trim, upcase, compbl, compress the data. 
2 REPLIES 2
ballardw
Super User

Didn't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the "</>" to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results (at least for a sample). Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the "</>" icon or attached as text to show exactly what you have and that we can test code against.

 

Warning: This forum seriously reformats text pasted into the main message windows. It will replace consecutive spaces with one (sometimes) other "white space" characters such as tabs as well. So any text you want to use for tests in string comparisons should be pasted into a text box. Really.

Code as well as sometimes strange non-printable characters appear that will prevent code from running correctly or at all.

 

Try adding STRIP(CITY) when using that as the search.

data work.MATCHING_CITY;
    set work.ADDR_DATA; 
    if index(FULL_ADDRESS, strip(CITY) ) > 0 then do;
       output;
    end;
run;

The behavior of INDEX, and most of the other string comparison functions, is that a variable will be padded with blanks to its defined length. Which means they are often way too long to find the target value in the source.

kojuramesh
Calcite | Level 5
Thank you for prompt answer, I added strip and code seems working now.

Thanks

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 2 replies
  • 390 views
  • 0 likes
  • 2 in conversation