BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jporterf
Fluorite | Level 6

Hello! I'm trying to combine two data sets into a single data set and I'm currently stuck. I imported both data sets into SAS as delimited raw data. 

 

Here is data set #1:

 

 Var1

 Var2

 Var3

 Var4

 Var5

1

-

-

-

-

-

2

-

-

-

-

-

3

-

-

-

-

-

 

And here is data set #2:

 

Var1

Var2

Var3

Var4

1

-

-

-

-

2

-

-

-

-

3

-

-

-

-

 

My output data is only showing the values for the CM data set. I need to concatenate and rename variables in the other data set, and also rename a variable for the CM data set. Do I need to rename and concatenate variables within each individual data set first, or is there's a way to do both actions within the data step I shared above? 

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

Try this - to append your datasets just use one SET statement then list your datasets:

DATA WORK.AllSites;
	RETAIN Institution;
	LENGTH	SexCd		$1.
			Institution	$11.
			Name		$15.;
	SET WORK.StJohns (RENAME	=	(	Site		=	Institution	
										PatientID	=	SubjID
										Sex		    =	SexCd)
					)
	      WORK.CM		(RENAME		=	(Gender	=	SexCd)	
						);
		Name = 	FirstNm||","||LastNm;
		DROP	FirstNm LastNm;
RUN;		

View solution in original post

5 REPLIES 5
SASKiwi
PROC Star

Try this - to append your datasets just use one SET statement then list your datasets:

DATA WORK.AllSites;
	RETAIN Institution;
	LENGTH	SexCd		$1.
			Institution	$11.
			Name		$15.;
	SET WORK.StJohns (RENAME	=	(	Site		=	Institution	
										PatientID	=	SubjID
										Sex		    =	SexCd)
					)
	      WORK.CM		(RENAME		=	(Gender	=	SexCd)	
						);
		Name = 	FirstNm||","||LastNm;
		DROP	FirstNm LastNm;
RUN;		
jporterf
Fluorite | Level 6

Thank you! I tried this and it worked! 

hhinohar
Quartz | Level 8
data have1;
	infile datalines dlm="09"x;
	input Site:$11. PatientID FirstNm $ LastNm $ Sex $;
	datalines;
St Johns	203	Daniel	Taylor	M
St Johns	206	Helen	Davis	F
St Johns	208	Betty	Smith	f
;
run;

data have2;
	infile datalines dlm="09"x;
	input Institution:$11. SubjID Name:$15. Gender $;
	datalines;
City Medics	102	Wilson, Steven	Male
City Medics	105	Moore, Chris	male
City Medics	109	Jackson, Sharon	Female
;
run;
*hash;
DATA _null_;
	if _N_=1 then
		do;
			RETAIN Institution;
			LENGTH Institution $11. SubjID 8 Name $15. sexcd $10. ;

			declare hash h(multidata:"y");
			h.definekey("Institution");
			h.definedata("Institution","SubjID","name","sexcd");
			h.definedone();
			call missing(subjid);
		end;

do until(last1);
	SET WORK.have1 (RENAME=(Site=Institution PatientID=SubjID sex=sexcd)) end=last1;
	Name=FirstNm||","||LastNm;
	h.add();
end;

do until(last2);
	set have2(RENAME=(Gender=SexCd)) end=last2;
	h.add();
end;

h.output(dataset:"want");

RUN;
*proc sql;
proc sql;
create table want as
	select a.institution,
	       a.subjid,
	       cats(a.firstnm,",",a.lastnm) as name,
	       a.sexcd
		from WORK.have1 (RENAME=(Site=Institution PatientID=SubjID sex=sexcd)) a
		union 
	select b.institution,
	       b.subjid,
	       b.name,
	       b.gender as sexcd 
		from work.have2 b;
quit;
jporterf
Fluorite | Level 6

Wow that's very detailed, thank you!

hhinohar
Quartz | Level 8

My pleasure. If you could attach your program in a running man icon with detailed input and expected output, more detailed answer will be posted in the future .

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1652 views
  • 3 likes
  • 3 in conversation