Desktop productivity for business analysts and programmers

How to find and replace using a lookup table

Accepted Solution Solved
Reply
Contributor
Posts: 54
Accepted Solution

How to find and replace using a lookup table

Hello, I have a table with a variable that stores Where clauses for SQL queries. I need to do a find/replace (like TRANWRD functionality) to this variable. The queries are pulled out of a 3rd party system and I need to change the field names in the where clause in order to run it against our local table. I have a lookup table that contains the string to find in one variable and the string to replace it with in another.

How can I replace each field name in one table by looking it up in another table and replacing it with the corresponding variable?

For example, The Query table looks like this:

QueryWhereClause
trancd = 8  or UI = 8
(trancd = 1 and herID = 'CD') or BWW = '3bth32'
(trancd = 2 and herID = 'CD') or BWW = '4bth10'

The lookuptable looks like this:

OldFieldNewField
trancdTRAN_CD
UIUSER_INT
herIDHER_ID_VC

What I need is the QueryWhereClause table to look like this:

Lookuptbl
TRAN_CD = 8  or USER_INT = 8
(TRAN_CD = 1 and HER_ID_VC= 'CD') or HER_ID_VC= '3bth32'
(TRAN_CD = 2 and HER_ID_VC= 'CD') or HER_ID_VC= '4bth10'


Thanks for looking!


Accepted Solutions
Solution
‎10-26-2011 01:08 PM
PROC Star
Posts: 7,439

How to find and replace using a lookup table

Here is a rather brute force way of accomplishing the task:

data querry;

  informat querry $50.;

  input querry &;

  cards;

trancd = 8 or UI = 8

(trancd = 1 and herID = 'CD') or BWW = '3bth32'

(trancd = 2 and herID = 'CD') or BWW = '4bth10'

;

data lookup;

  informat old new $10.;

  input (old new) ($);

  cards;

trancd          TRAN_CD

UI          USER_INT

herID          HER_ID_VC

;

/*** get number of records in lookup dataset ***/

data _null_;

  if 0 then set lookup nobs=nobs;  /*** no need to read dataset - just metadata ***/

  CALL SYMPUT('NUMREC',nobs);      /*** put # of records into NUMREC macro var ***/

  stop;                            /*** stop, got number of records ***/

run;

/*** now read the lookup records into an array, and then read the querry records

     and modify with tranwrd ***/

data want (keep=querry);

  array Lookup(2,&numrec) $10; /*** create an array with same number of elements

                                  as there are records in amaster ***/

  i=0;

  do until (eof1);           /*** load the array with the lookup records ***/

    set lookup end=eof1;

    i+1;

    lookup(1,i)=old;

    lookup(2,i)=new;

  end;

  do until (eof2); /*** now read each querry record and apply tranwrd function***/

    set querry end=eof2;

    do i=1 to &numrec;

      querry=tranwrd(querry,strip(lookup(1,i)),strip(lookup(2,i)));

    end;

    output;

  end;

run;

View solution in original post


All Replies
Solution
‎10-26-2011 01:08 PM
PROC Star
Posts: 7,439

How to find and replace using a lookup table

Here is a rather brute force way of accomplishing the task:

data querry;

  informat querry $50.;

  input querry &;

  cards;

trancd = 8 or UI = 8

(trancd = 1 and herID = 'CD') or BWW = '3bth32'

(trancd = 2 and herID = 'CD') or BWW = '4bth10'

;

data lookup;

  informat old new $10.;

  input (old new) ($);

  cards;

trancd          TRAN_CD

UI          USER_INT

herID          HER_ID_VC

;

/*** get number of records in lookup dataset ***/

data _null_;

  if 0 then set lookup nobs=nobs;  /*** no need to read dataset - just metadata ***/

  CALL SYMPUT('NUMREC',nobs);      /*** put # of records into NUMREC macro var ***/

  stop;                            /*** stop, got number of records ***/

run;

/*** now read the lookup records into an array, and then read the querry records

     and modify with tranwrd ***/

data want (keep=querry);

  array Lookup(2,&numrec) $10; /*** create an array with same number of elements

                                  as there are records in amaster ***/

  i=0;

  do until (eof1);           /*** load the array with the lookup records ***/

    set lookup end=eof1;

    i+1;

    lookup(1,i)=old;

    lookup(2,i)=new;

  end;

  do until (eof2); /*** now read each querry record and apply tranwrd function***/

    set querry end=eof2;

    do i=1 to &numrec;

      querry=tranwrd(querry,strip(lookup(1,i)),strip(lookup(2,i)));

    end;

    output;

  end;

run;

Contributor
Posts: 54

How to find and replace using a lookup table

Jaw dropping!

I just had to change the array length to be big enough to fit all of my lookup values in (from

"array Lookup(2,&numrec) $10" I changed $10 to $100) and I was good to go!

Strange results happen when that is too small or too big.

Thank you so much!!!

PROC Star
Posts: 7,439

How to find and replace using a lookup table

Sometimes brute force is good!  Minimally, it can often accomplish a task which can them be optimized when/if you ever discover a better method.

🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 423 views
  • 0 likes
  • 2 in conversation