BookmarkSubscribeRSS Feed
mkeintz
PROC Star

Here is how I would do it in SQL, although I prefer the hash approach:

 

proc sql;
  create view _3gens as select h.*, h2.parent as grandparent 
   from hierarchy as h
   inner join 
        hierarchy as h2
   on h.parent=h2.child;

  create table want as select _3gens.*, h3.parent as greatgrandparent
   from _3gens
   inner join
       hierarchy as h3
  on _3gens.grandparent=h3.child;
quit;

Or, if you want to do it all in one statement:

 

proc sql;
  create table want as select _3gens.*,h3.parent as greatgrandparent from
   (select h.*, h2.parent as grandparent from
      hierarchy as h   inner join 
      hierarchy as h2  on h.parent=h2.child) 
   as _3gens
   inner join
     hierarchy as h3
  on _3gens.grandparent=h3.child;
quit;

But imagine how ugly this would look if you wanted, say 6 generations.  The hash coding approach would be far easier to expand.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 4683 views
  • 8 likes
  • 6 in conversation