DATA Step, Macro, Functions and more

Data Step IN operator

Accepted Solution Solved
Reply
Trusted Advisor
Posts: 1,908
Accepted Solution

Data Step IN operator

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.


Accepted Solutions
Solution
‎08-02-2013 01:54 PM
Respected Advisor
Posts: 3,156

Re: Data Step IN operator

Posted in reply to PaigeMiller

Have you tried Findw() function? Of course, PRXMatch can do it as well.

Haikuo

View solution in original post


All Replies
Solution
‎08-02-2013 01:54 PM
Respected Advisor
Posts: 3,156

Re: Data Step IN operator

Posted in reply to PaigeMiller

Have you tried Findw() function? Of course, PRXMatch can do it as well.

Haikuo

Trusted Advisor
Posts: 1,908

Re: Data Step IN operator

Yes, thank you and , FINDW was exactly what I was looking for.

Respected Advisor
Posts: 4,918

Re: Data Step IN operator

Posted in reply to PaigeMiller

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

PG
Occasional Contributor
Posts: 9

Re: Data Step IN operator

Posted in reply to PaigeMiller

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;

Trusted Advisor
Posts: 1,908

Re: Data Step IN operator

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.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 226 views
  • 3 likes
  • 4 in conversation