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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 15 replies
  • 29353 views
  • 6 likes
  • 8 in conversation