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;
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=_:) n=total;
run;
data check2; set check1;
percent=100*total/&nbirds;
format percent 5.1;
run;
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;
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;
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=_:) n=total;
run;
data check2; set check1;
percent=100*total/&nbirds;
format percent 5.1;
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.