DATA Step, Macro, Functions and more

Compare two data sets

Reply
Occasional Contributor
Posts: 10

Compare two data sets

Hello Friends,

I want to search if a observation (date) in data set B1 is in data set A1 or not.

If obv in A1 found then print "found" else append the observation of B1 into A1.

data a1;

input date_file $;

datalines;

24NOV2011

25NOV2011

26NOV2011

27NOV2011

28NOV2011

;

data b1;

input date_file $;

datalines;

26NOV2011

30NOV2011

;

I try to use PROC COMPARE but it's not giving desirable result. Please help me out.

Thanks

Rohan,

Respected Advisor
Posts: 3,124

Re: Compare two data sets

Assume they are both sorted, then we can have:

data a1;

merge a1(in=a) b1(in=b);

by date_file;

if a then output;

if a and b then put "Found=" date_file;

else if b and not a then output;

run;

Regards,

Haikuo

N/A
Posts: 1

Re: Compare two data sets

I Have to create datasets with sysmonth, sysmonth-1 & Sysmonth-2 as the names, how can i do that?

PROC Star
Posts: 7,363

Re: Compare two data sets

Vijay, Since your screen name is different than the original poster's name, I'd suggest that you start a new discussion, provide an example "have" data set and an example "want" dataset.

Super Contributor
Posts: 1,636

Re: Compare two data sets

show off my newly acquired hash skills:

data a1;

input date_file $;

datalines;

24NOV2011

25NOV2011

26NOV2011

27NOV2011

28NOV2011

;

data b1;

input date_file $;

datalines;

26NOV2011

30NOV2011

;

data _null_;

  if _n_=1 then do;

  declare hash ha(dataset:'b1', ordered: 'yes');

    ha.definekey('date_file');

          ha.definedata('date_file');

          ha.definedone();

  end;

          set a1 ;

          rc=ha.find();

          if rc = 0 then put "Found=" date_file;

          else ha.add();

  ha.output(dataset:'ai_updated');

run;

Linlin

Ask a Question
Discussion stats
  • 4 replies
  • 211 views
  • 0 likes
  • 5 in conversation