BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

Haikuo

View solution in original post

5 REPLIES 5
Haikuo
Onyx | Level 15

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

Haikuo

PaigeMiller
Diamond | Level 26

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

--
Paige Miller
PGStats
Opal | Level 21

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
Kanna
Calcite | Level 5

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;

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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