Help using Base SAS procedures

PROC COMPARE - Keep New or Changed Observations

Reply
Regular Contributor
Posts: 237

PROC COMPARE - Keep New or Changed Observations

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
Super User
Posts: 5,518

Re: PROC COMPARE - Keep New or Changed Observations

Posted in reply to djbateman

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.

Regular Contributor
Posts: 237

Re: PROC COMPARE - Keep New or Changed Observations

Posted in reply to Astounding

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

Super User
Posts: 19,878

Re: PROC COMPARE - Keep New or Changed Observations

Posted in reply to djbateman

Please upvote this idea Smiley Happy

Proc Compare - ODS Table Output Enhance

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

Ask a Question
Discussion stats
  • 3 replies
  • 178 views
  • 0 likes
  • 3 in conversation