I would like to use the data step IN operator where the value inside parenthesis to the right of the word IN is itself a variable in that data set.
For example, I receive a transaction code, and I want to determine if the code is contained in a list of allowable codes.
So, here is some example data
TransactionCode Date AllowableCodes
206 1/12/11 213,214,215
209 1/19/11 208,209,210,228
255 2/17/11 213,214,229
I would like to compare the TransactionCode to AllowableCodes, and if that transaction code is contained within AllowableCodes, I take some action. In this case, only the second row of the data would pass the test (transaction code 209 is contained within AllowableCodes). The other rows would not pass the test.
I would like to write a data step command such as
if transactioncode in (allowablecodes) then flag=1;
but that syntax doesn't work.
I'm looking for a simple way to accomplish this, if it exists. I realize I could write a loop, but that's problematic for other reasons that I won't get into right now.
Have you tried Findw() function? Of course, PRXMatch can do it as well.
Haikuo
Have you tried Findw() function? Of course, PRXMatch can do it as well.
Haikuo
That's not likely to work anytime soon, as the IN operator doesn't even work with numeric variables in the list! I would use :
data test;
input TransactionCode Date :mmddyy. AllowableCodes :$20.;
format date date9.;
datalines;
206 1/12/11 213,214,215
209 1/19/11 208,209,210,228
255 2/17/11 213,214,229
;
data want;
set test;
flag = findw(AllowableCodes, strip(put(TransactionCode, 8.)), ", ") > 0;
run;
proc print data=want; run;
PG
Paige, try this:
data null;
List206 = " '213' , '214' , '215' ";
call symput("List206",List206);
run;
data locate206;
set myinfile;
if TransactionCode = '206' and AllowableCode in (&List206) then do;
output;
end;
run;
Kanna wrote:
Paige, try this:
data null;
List206 = " '213' , '214' , '215' ";
call symput("List206",List206);
run;data locate206;
set myinfile;
if TransactionCode = '206' and AllowableCode in (&List206) then do;
output;
end;
run;
Not a workable answer. For this to work, you would have to know in advance of the working with the dataset what the list of allowable codes is, and as shown, the list of allowable codes changes for every observation.
FINDW works perfectly.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.