BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
FriedEgg
SAS Employee

I would never recommend actually using this approach as it is far overly complicated, fairly inflexible and the worst performing of all the suggestions:

proc fcmp outlib=work.func.excl;

   function vlookup(dsname $, idvar $, id, col_index) $ 200;

      length val $ 200;

      dsn=cats(dsname, '(where=(', idvar, '=', id, '))');

      dsid=open(dsn);

      if dsid=0 then do;

         put '4552524f523a20'x 'No lookup value for ' id=;

         return ('');

      end;

      vartype=vartype(dsid, col_index);

      rc=fetch(dsid);

      if rc ne 0 then do;

         msg=sysmsg();

         put msg;

         return ('');

      end;

      if vartype='C' then do;

         val=getvarc(dsid, col_index);

      end;

      else do;

         varfmt=varfmt(dsid, col_index);

         val=putn(getvarn(dsid, col_index), varfmt);

      end;

      rc=close(dsid);

      return (val);

   endsub;

run;

options cmplib=work.func;

data have;

input id name $14. report_to;

cards;

200000 Andrew, Skype  000006

520001 Walker, Walker 000006

200000 Andrew, Skype  000006

520001 Walker, Walker 000006

000006 Smith, John    000007

000007 Phil, Elias    000010

000008 Turner, Sandra 200000

000010 Santa, Clause  200000

;

data want;

set have;

report_to_name=vlookup('have', 'id', report_to, 2);

run;

      

obsidnamereport_toreports_to_name
1200000Andrew, Skype  6Smith, John
2520001Walker, Walker  6Smith, John
3200000Andrew, Skype  6Smith, John
4520001Walker, Walker  6Smith, John
5  6Smith, John  7Phil, Elias
6  7Phil, Elias  10Santa, Clause
7  8Turner, Sandra200000Andrew, Skype
8  10Santa, Clause200000Andrew, Skype

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 15 replies
  • 33091 views
  • 6 likes
  • 8 in conversation