BookmarkSubscribeRSS Feed
djbateman
Lapis Lazuli | Level 10

I have a dataset that I created last week, and I have a new dataset that I created this week.  I want to compare the two tables and only keep the observations in the new table that are either new or had a change.  Is there a way to retain the ID values that correspond to any new or changed observations?  For example, in the datasets below, I would like to create a new table that only keeps IDs 003 (added SEX and changed LASTVISIT), 005 (changed LASTVISIT), 006 (new observation), and 007 (new observation).  I have looked through the documentation to see what my options are with output tables, but the output tables are not very usable for my needs.

data old;

            input ID $ AGE SEX $ 9-10 LASTVISIT;

            informat lastvisit date9.;

            format lastvisit date9.;

            cards;

001       37         M         15JAN2015

002       29         M         29JAN2015

003       64                      12FEB2015

004       52         M         24FEB2015

005       49         F          20FEB2015

;

run;

data new;

            input ID $ AGE SEX $ LASTVISIT;

            informat lastvisit date9.;

            format lastvisit date9.;

            cards;

001       37         M         15JAN2015

002       29         M         29JAN2015

003       64         F          12FEB2015

004       52         M         24FEB2015

005       49         F           27FEB2015

006       50         F          26FEB2015

007       56         F          27FEB2015

;

run;

Desired Results:

IDAGESEXLASTVISIT
00364F12FEB2015
00549F27FEB2015
00650F26FEB2015
00756F27FEB2015
3 REPLIES 3
Astounding
PROC Star

PROC COMPARE doesn't have a method that I know of, but you can certainly program it yourself.  For example:

data want;

   set old (in=in1) new (in=in2);

   by id;

   prior_age = lag(age);

   prior_sex = lag(sex);

   prior_lastvisit = lag(lastvisit);

   if in2;

   if (age ne prior_age) or (sex ne prior_sex) or (lastvisit ne prior_lastvisit);

   * optionally:  drop prior_:;

run;

The code assumes that both data sets are sorted (easy), but can become cumbersome to program if there are a lot of variables.

Good luck.

djbateman
Lapis Lazuli | Level 10

Yes, I have already written a program to compare them myself.  I was just hoping for a more efficient way.  But thank you anyway!

Reeza
Super User

Please upvote this idea Smiley Happy

Proc Compare - ODS Table Output Enhance

https://communities.sas.com/ideas/1734

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 673 views
  • 0 likes
  • 3 in conversation