Help using Base SAS procedures

How to merge datasets and print a summary?

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

How to merge datasets and print a summary?

I have a couple of datasets.  One has and ID and defect code.  The other defines the defect codes.

My question is how do I merge these so that I can print a summary of the defects using the

defect description?  I've put some code together but my code is quickly getting more complicated

than something like this should be.  Would someone please steer me in the right direction?

Here is the code:

DATA FCRALL;

   infile datalines dsd;

   input id defect1;

   datalines;

111,1

222,1

333,2

444,0

;

DATA DEFECTS;

   infile datalines dsd;

   input defect1 description $20.;

   datalines;

1,Dead on Arrival

2,Welfare cull

3,Extra Bird Cull

4,Direct to Growout

;

PROC SORT DATA=DEFECTS; BY DEFECT1;

PROC SORT DATA=FCRALL; BY DEFECT1;

DATA CHECK3; MERGE FCRALL DEFECTS; BY DEFECT1;

PROC PRINT; WHERE DEFECT1>0;

VAR id DESCRIPTION;

TITLE DEFECTS FOR LINE;

/*

* Get the total number observations in fcrall

*/

data _null_;

   if 0 then set fcrall nobs=nobs;

   call symputx("nbirds",nobs);

   stop;

   run;

proc summary data=check3; by defect1;

   var defect1;

   output out=check1 n=total;

data check2; set check1;

   percent=100*total/&nbirds;

   format percent 5.1;

proc print noobs;

   var defect1 total percent;


Accepted Solutions
Solution
‎12-06-2011 04:09 PM
PROC Star
Posts: 7,416

How to merge datasets and print a summary?

Not sure if I correctly understand what you are trying to do.  The following gets rid of the extra records by adding the in option on the merge datastep and brings the descriptions along (in your proc summary) using an id statement:

DATA FCRALL;

   infile datalines dsd;

   input id defect1;

   datalines;

111,1

222,1

333,2

444,0

;

DATA DEFECTS;

   infile datalines dsd;

   input defect1 description $20.;

   datalines;

1,Dead on Arrival

2,Welfare cull

3,Extra Bird Cull

4,Direct to Growout

;

PROC SORT DATA=DEFECTS; BY DEFECT1;run;

PROC SORT DATA=FCRALL; BY DEFECT1;run;

DATA CHECK3;

  MERGE FCRALL (in=in_a) DEFECTS;

  BY DEFECT1;

  if in_a;

run;

data _null_;

   if 0 then set fcrall nobs=nobs;

   call symputx("nbirds",nobs);

   stop;

run;

proc summary data=check3; by defect1;

   var defect1;

   id description;

   output out=check1 (drop=_Smiley Happy n=total;

run;

data check2; set check1;

   percent=100*total/&nbirds;

   format percent 5.1;

run;

View solution in original post


All Replies
Super Contributor
Posts: 275

How to merge datasets and print a summary?

Try this. the SQL step take all obs in the first data set and joins them to all matches in the 2nd data set.  I also got rid of dsd and used dlm=',' instead.

DATA FCRALL;

   infile datalines dlm=',';

   input id defect1;

   datalines;

111,1

222,1

333,2

444,0

;

run;

DATA DEFECTS;

   infile datalines dlm=',';

   input defect1 description $20.;

   datalines;

1,Dead on Arrival

2,Welfare cull

3,Extra Bird Cull

4,Direct to Growout

;

run;

proc sql;

create table join1 as

select f.id, f.defect1, d.description from FCRALL as f

left join DEFECTS as d

on f.defect1=d.defect1;

quit;

Contributor
Posts: 65

How to merge datasets and print a summary?

I have changed your initial code.

DATA FCRALL;

   infile datalines dsd;

   input id defect1;

   datalines;

111,1

222,1

333,2

444,0

;

DATA DEFECTS;

   infile datalines dsd;

   input defect1 description $20.;

   datalines;

1,Dead on Arrival

2,Welfare cull

3,Extra Bird Cull

4,Direct to Growout

;

PROC SORT DATA=DEFECTS; BY DEFECT1;

PROC SORT DATA=FCRALL; BY DEFECT1;

/* This is what you can do  and hope it is helpful */

data want;

merge fcrall (in = a) defects (in = b);

by defect1;

/* If you want only the matching col */

if a and b;

/* If you need to a left join as Gregg pointed out use

if a; */

run;

Solution
‎12-06-2011 04:09 PM
PROC Star
Posts: 7,416

How to merge datasets and print a summary?

Not sure if I correctly understand what you are trying to do.  The following gets rid of the extra records by adding the in option on the merge datastep and brings the descriptions along (in your proc summary) using an id statement:

DATA FCRALL;

   infile datalines dsd;

   input id defect1;

   datalines;

111,1

222,1

333,2

444,0

;

DATA DEFECTS;

   infile datalines dsd;

   input defect1 description $20.;

   datalines;

1,Dead on Arrival

2,Welfare cull

3,Extra Bird Cull

4,Direct to Growout

;

PROC SORT DATA=DEFECTS; BY DEFECT1;run;

PROC SORT DATA=FCRALL; BY DEFECT1;run;

DATA CHECK3;

  MERGE FCRALL (in=in_a) DEFECTS;

  BY DEFECT1;

  if in_a;

run;

data _null_;

   if 0 then set fcrall nobs=nobs;

   call symputx("nbirds",nobs);

   stop;

run;

proc summary data=check3; by defect1;

   var defect1;

   id description;

   output out=check1 (drop=_Smiley Happy n=total;

run;

data check2; set check1;

   percent=100*total/&nbirds;

   format percent 5.1;

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 171 views
  • 6 likes
  • 4 in conversation