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

hi,

I need help while merging multiple datasets. I have 6 datasets as following:

 

Dataset a        
idagein_2012      
130Y      
Dataset b        
idagein_2013      
         
Dataset c        
idagein_2014      
132Y      
Dataset d        
idagein_2015      
133Y      
Dataset e        
idagein_2016      
134Y      
Dataset f        
idagein_2017      
135Y      
Dataset g        
idagein_2018      
136Y      
         
Final output       
idagein_2012in_2013in_2014in_2015in_2016in_2017in_2018
130Y      
132  Y    
133   Y   
134    Y  
135     Y 
136      Y

Subject Id 1 has record present from 2012 to 2018 hence we have variable in_2012... to in_2018.

When i am merging the above 6 datasets by id and age , i am getting the above output.

Can anyone pl guide me how can i get the output with the variable in_2012 to in_2018 has "Y" in all the rows(if records are available in all datasets) and not in selective rows as presented above.

This is the ouput which i want:

idagein_2012in_2013in_2014in_2015in_2016in_2017in_2018
130Y YYYYY
132Y YYYYY
133Y YYYYY
134Y YYYYY
135Y YYYYY
136Y YYYYY

 

Thanks,

Jeetender

1 ACCEPTED SOLUTION

Accepted Solutions
sotojcr
Obsidian | Level 7
data A;
input ID AGE IN_2012 $1.;
datalines;
1 30 Y
;
run;

data B;
input ID AGE IN_2013 $1.;
datalines;
;
run;

data C;
input ID AGE IN_2014 $1.;
datalines;
1 32 Y
;
run;

data D;
input ID AGE IN_2015 $1.;
datalines;
1 33 Y
;
run;

data E;
input ID AGE IN_2016 $1.;
datalines;
1 34 Y
;
run;

data F;
input ID AGE IN_2017 $1.;
datalines;
1 35 Y
;
run;

data G;
input ID AGE IN_2018 $1.;
datalines;
1 36 Y
;
run;
data  A_TO_G;
set  A B C D E F G;
keep ID AGE;
run;

proc sort data=A_TO_G nodupkey;
by ID AGE;
run;

proc sort data=A;
by ID AGE;
run;

proc sort data=B;
by ID AGE;
run;

proc sort data=C;
by ID AGE;
run;

proc sort data=D;
by ID AGE;
run;

proc sort data=E;
by ID AGE;
run;

proc sort data=F;
by ID AGE;
run;

proc sort data=G;
by ID AGE;
run;

data ALL_TABLE;
merge A_TO_G(in=a)
	  A(drop=age)
	  B(drop=age)
	  C(drop=age)
	  D(drop=age)
	  E(drop=age)
	  F(drop=age)
	  G(drop=age);
by ID;
if a;
run;

View solution in original post

3 REPLIES 3
sotojcr
Obsidian | Level 7
data A;
input ID AGE IN_2012 $1.;
datalines;
1 30 Y
;
run;

data B;
input ID AGE IN_2013 $1.;
datalines;
;
run;

data C;
input ID AGE IN_2014 $1.;
datalines;
1 32 Y
;
run;

data D;
input ID AGE IN_2015 $1.;
datalines;
1 33 Y
;
run;

data E;
input ID AGE IN_2016 $1.;
datalines;
1 34 Y
;
run;

data F;
input ID AGE IN_2017 $1.;
datalines;
1 35 Y
;
run;

data G;
input ID AGE IN_2018 $1.;
datalines;
1 36 Y
;
run;
data  A_TO_G;
set  A B C D E F G;
keep ID AGE;
run;

proc sort data=A_TO_G nodupkey;
by ID AGE;
run;

proc sort data=A;
by ID AGE;
run;

proc sort data=B;
by ID AGE;
run;

proc sort data=C;
by ID AGE;
run;

proc sort data=D;
by ID AGE;
run;

proc sort data=E;
by ID AGE;
run;

proc sort data=F;
by ID AGE;
run;

proc sort data=G;
by ID AGE;
run;

data ALL_TABLE;
merge A_TO_G(in=a)
	  A(drop=age)
	  B(drop=age)
	  C(drop=age)
	  D(drop=age)
	  E(drop=age)
	  F(drop=age)
	  G(drop=age);
by ID;
if a;
run;
ballardw
Super User

I'm sort of interested in how you are going to use that output file.

 

The structure and variable names makes me think that sometime in 2019 or 2020 you will be adding more columns. Which means any code processing this data set will need to account for the added column a may become cumbersome.

 

This type of structure to a data set is pretty common to someone attempting to replicate a process that had been done in a spreadsheet and frequently is suboptimal when using other software.

Astounding
PROC Star

Assuming your data sets are all sorted:

 

data combined;
   set a b c d e f g;
by id; run; data years; update combined (obs=0 drop=age) combined (drop=age); by id; run; data want; merge combined (keep=id age) years; by id; run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 794 views
  • 2 likes
  • 4 in conversation