Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 935185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:07:05+00:00 2026-05-15T21:07:05+00:00

Say I’ve already sorted set1 and set2 by the variables ‘sticks’, ‘stones’, and ‘bones’

  • 0

Say I’ve already sorted set1 and set2 by the variables ‘sticks’, ‘stones’, and ‘bones’ and then i do this:

data merged;
    merge set1(in=a) set2(in=b);
    by sticks stones bones;
    if a and b then output;
    *else we don't want to do anything;
run;

Is there an easy way to drop all the variables from set2 in the merged dataset without having to type them all? I keep running into this problem where I have two datasets – both with quite a few variables – and I only want to merge them by a few variables and then only keep the variables from one of the sets.

I usually just use proc sql for something like this, but there are a few situations (more complex than above) where where I think merge is better.

Also, I find it annoying that SAS requires you to “manually” sort datasets before merging them. If it will not let you merge datasets unless they are sorted correctly, why doesn’t it just do it for you when you use merge? Thoughts? Maybe there is a way around this I don’t know about.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:07:06+00:00Added an answer on May 15, 2026 at 9:07 pm

    The sorted requirements is there for the way the merge statement and the PDV works in it.
    There is really no way around it.

    However here basically you’re doing a lookup of set2 to make sure you have a match of the key variables (sticks stones bones) through the equivalent of an inner join, which you could likely do more efficiently through an hash table or set with keys (if you have an index of course).

    The easiest and more convenient way for what you want here is having a keep statement in the set2 so you load into the PDV only the by variables.
    Something like this:

    data merged;
    merge set1(in=a) set2(in=b keep=sticks stones bones);
    by sticks stones bones;
    if a and b then output;
    *else we don't want to do anything;
    
    run;
    

    In case hash tables don’t scare you and want to learn more on how to implement them in this case feel free to contact me for more help.

    EDIT:

    Here is a good paper about using hash tables http://www.nesug.org/proceedings/nesug06/dm/da07.pdf
    Bear in mind that using hashes you should know what you’re doing and they may yield unexpected results if you don’t know whats happening under the hood.
    Regardless here is the problem solved using a very simple and basic hash table

    data merged2;
    set set1;
    if _N_ = 1 then do;
      declare hash h(dataset:"set2");
      h.defineKey('sticks','stones','bones');
      h.defineData('sticks','stones','bones');
      h.defineDone();
    end;
    rc = h.find();
    if rc=0;
    drop rc;
    run;
    

    This code has the main benefit of not requiring the sorting of the datasets which in case set2 is particularly big is a great time-saver.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a select box eg <div data-bind='visible: someProp'> <select class=selectSubWidgets data-bind='options: subWidgets,optionsText:
Say I have this: private list<myClass> myCollection; Is there a programming idiom to shorten
Say I have a SqlAlchemy model something like this: from sqlalchemy.ext.declarative import declarative_base from
Say I want to test this module: import osutils def check_ip6(xml): ib_output = osutils.call('iconfig
say I have input data like so: firstName | lastName | Country Bob |
Say I have a class pet and it is composed of two variables: Class
Say I have a LINQ-to-XML query that generates an anonymous type like this: var
say i have the 3 non blocking sends like this MPI_Isend (); MPI_Isend ();
Say, if we already have aScreen that points to the main screen, by UIScreen
Say I have this: my %hash; $hash{a} = abc; $hash{b} = [1, 2, 3];

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.