ID | Visited | Loc1 | Loc2 | Flag |
001 | Asia,Africa | Asia | Y | |
001 | Europe,USA, UK | UK | Y | |
001 | Europe,Australia | China | Japan | N |
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.
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)
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.
Yes, Locaiton1 and Location2 are Loc1 and Loc2. I updated names now in question.
I am not seeing any warnings or errors.
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)
Now I have ten locations. How can I write for multiple location variables?
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;
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.