BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SAS_Learner2
Calcite | Level 5

 

IDVisitedLoc1Loc2Flag
001Asia,Africa AsiaY
001Europe,USA, UKUK Y
001Europe,AustraliaChinaJapanN

I want create Flag variable whenever if Locaiton1 or Location2 having one of the location in Visited variable.

 

I am using below code. But not working.

 

data want;
set have;
Array loc $ loc1-loc2;
do i = 1 to 2;
if index(Visited, loc[i])>0 then flag = "Y";
end;
Run;

 

Please help.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data want;
    set have;
    if findw(visited,trim(location1))>0 or findw(visited,trim(location2))>0 then flag=1;
    else flag=0;
run;

 

 

Now, if the real problem has LOCATION1 LOCATION2, ..., LOCATION73 we can probably give better code in that case. In addition, the TRIM command may not be necessary, depending on how your data actually exists in the SAS data set (we don't know and can't tell from the Excel-like posting of data, and we normally require data posted as working SAS data step code)

--
Paige Miller

View solution in original post

5 REPLIES 5
ballardw
Super User

Are the variable names Location1 and Location2 as shown in the example or Loc1 and Loc2 as in the code for the Array?

 

"Not working" 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. 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.

SAS_Learner2
Calcite | Level 5

Yes, Locaiton1 and Location2 are Loc1 and Loc2. I updated names now in question. 

I am not seeing any warnings or errors.

 

PaigeMiller
Diamond | Level 26
data want;
    set have;
    if findw(visited,trim(location1))>0 or findw(visited,trim(location2))>0 then flag=1;
    else flag=0;
run;

 

 

Now, if the real problem has LOCATION1 LOCATION2, ..., LOCATION73 we can probably give better code in that case. In addition, the TRIM command may not be necessary, depending on how your data actually exists in the SAS data set (we don't know and can't tell from the Excel-like posting of data, and we normally require data posted as working SAS data step code)

--
Paige Miller
SAS_Learner2
Calcite | Level 5

Now I have ten locations. How can I write for multiple location variables?

 

PaigeMiller
Diamond | Level 26
data want;
    set have;
    array loc location1-location10;
    flag=0;
    do i=1 to dim(loc);
        if findw(visited,trim(loc(i)))>0 then flag=1;
    end;
    drop i;
run;
--
Paige Miller

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