When you combine datasets using the "set" statement, do the variables in each of the datasets have to correspond to each other? In other words, do the same variables have to have the same name? For example, if the variable for systolic blood pressure at baseline is sbp in one dataset, would I have to name the variable for systolic blood pressure for another dataset sbp, even if it's the systolic blood pressure at follow-up? Thanks!
Please review this thoroughly, you will understand:
data one;
set sashelp.class;
run;
data two;
set sashelp.class;
run;
title 'append with same_varnames';
data same_varnames;
set one two;
run;
proc print data=same_varnames;run;
title ;
/*rename to make it different*/
data two;
set sashelp.class;
rename height=ht;
run;
title 'append with diff_varname';
data diff_varname;
set one two;
run;
proc print data=diff_varname;run;
Outputs:
SAS Output
append with same_varnames |
Alfred | M | 14 | 69.0 | 112.5 |
Alice | F | 13 | 56.5 | 84.0 |
Barbara | F | 13 | 65.3 | 98.0 |
Carol | F | 14 | 62.8 | 102.5 |
Henry | M | 14 | 63.5 | 102.5 |
James | M | 12 | 57.3 | 83.0 |
Jane | F | 12 | 59.8 | 84.5 |
Janet | F | 15 | 62.5 | 112.5 |
Jeffrey | M | 13 | 62.5 | 84.0 |
John | M | 12 | 59.0 | 99.5 |
Joyce | F | 11 | 51.3 | 50.5 |
Judy | F | 14 | 64.3 | 90.0 |
Louise | F | 12 | 56.3 | 77.0 |
Mary | F | 15 | 66.5 | 112.0 |
Philip | M | 16 | 72.0 | 150.0 |
Robert | M | 12 | 64.8 | 128.0 |
Ronald | M | 15 | 67.0 | 133.0 |
Thomas | M | 11 | 57.5 | 85.0 |
William | M | 15 | 66.5 | 112.0 |
Alfred | M | 14 | 69.0 | 112.5 |
Alice | F | 13 | 56.5 | 84.0 |
Barbara | F | 13 | 65.3 | 98.0 |
Carol | F | 14 | 62.8 | 102.5 |
Henry | M | 14 | 63.5 | 102.5 |
James | M | 12 | 57.3 | 83.0 |
Jane | F | 12 | 59.8 | 84.5 |
Janet | F | 15 | 62.5 | 112.5 |
Jeffrey | M | 13 | 62.5 | 84.0 |
John | M | 12 | 59.0 | 99.5 |
Joyce | F | 11 | 51.3 | 50.5 |
Judy | F | 14 | 64.3 | 90.0 |
Louise | F | 12 | 56.3 | 77.0 |
Mary | F | 15 | 66.5 | 112.0 |
Philip | M | 16 | 72.0 | 150.0 |
Robert | M | 12 | 64.8 | 128.0 |
Ronald | M | 15 | 67.0 | 133.0 |
Thomas | M | 11 | 57.5 | 85.0 |
William | M | 15 | 66.5 | 112.0 |
append with diff_varname |
Alfred | M | 14 | 69.0 | 112.5 | . |
Alice | F | 13 | 56.5 | 84.0 | . |
Barbara | F | 13 | 65.3 | 98.0 | . |
Carol | F | 14 | 62.8 | 102.5 | . |
Henry | M | 14 | 63.5 | 102.5 | . |
James | M | 12 | 57.3 | 83.0 | . |
Jane | F | 12 | 59.8 | 84.5 | . |
Janet | F | 15 | 62.5 | 112.5 | . |
Jeffrey | M | 13 | 62.5 | 84.0 | . |
John | M | 12 | 59.0 | 99.5 | . |
Joyce | F | 11 | 51.3 | 50.5 | . |
Judy | F | 14 | 64.3 | 90.0 | . |
Louise | F | 12 | 56.3 | 77.0 | . |
Mary | F | 15 | 66.5 | 112.0 | . |
Philip | M | 16 | 72.0 | 150.0 | . |
Robert | M | 12 | 64.8 | 128.0 | . |
Ronald | M | 15 | 67.0 | 133.0 | . |
Thomas | M | 11 | 57.5 | 85.0 | . |
William | M | 15 | 66.5 | 112.0 | . |
Alfred | M | 14 | . | 112.5 | 69.0 |
Alice | F | 13 | . | 84.0 | 56.5 |
Barbara | F | 13 | . | 98.0 | 65.3 |
Carol | F | 14 | . | 102.5 | 62.8 |
Henry | M | 14 | . | 102.5 | 63.5 |
James | M | 12 | . | 83.0 | 57.3 |
Jane | F | 12 | . | 84.5 | 59.8 |
Janet | F | 15 | . | 112.5 | 62.5 |
Jeffrey | M | 13 | . | 84.0 | 62.5 |
John | M | 12 | . | 99.5 | 59.0 |
Joyce | F | 11 | . | 50.5 | 51.3 |
Judy | F | 14 | . | 90.0 | 64.3 |
Louise | F | 12 | . | 77.0 | 56.3 |
Mary | F | 15 | . | 112.0 | 66.5 |
Philip | M | 16 | . | 150.0 | 72.0 |
Robert | M | 12 | . | 128.0 | 64.8 |
Ronald | M | 15 | . | 133.0 | 67.0 |
Thomas | M | 11 | . | 85.0 | 57.5 |
William | M | 15 | . | 112.0 | 66.5 |
Please review this thoroughly, you will understand:
data one;
set sashelp.class;
run;
data two;
set sashelp.class;
run;
title 'append with same_varnames';
data same_varnames;
set one two;
run;
proc print data=same_varnames;run;
title ;
/*rename to make it different*/
data two;
set sashelp.class;
rename height=ht;
run;
title 'append with diff_varname';
data diff_varname;
set one two;
run;
proc print data=diff_varname;run;
Outputs:
SAS Output
append with same_varnames |
Alfred | M | 14 | 69.0 | 112.5 |
Alice | F | 13 | 56.5 | 84.0 |
Barbara | F | 13 | 65.3 | 98.0 |
Carol | F | 14 | 62.8 | 102.5 |
Henry | M | 14 | 63.5 | 102.5 |
James | M | 12 | 57.3 | 83.0 |
Jane | F | 12 | 59.8 | 84.5 |
Janet | F | 15 | 62.5 | 112.5 |
Jeffrey | M | 13 | 62.5 | 84.0 |
John | M | 12 | 59.0 | 99.5 |
Joyce | F | 11 | 51.3 | 50.5 |
Judy | F | 14 | 64.3 | 90.0 |
Louise | F | 12 | 56.3 | 77.0 |
Mary | F | 15 | 66.5 | 112.0 |
Philip | M | 16 | 72.0 | 150.0 |
Robert | M | 12 | 64.8 | 128.0 |
Ronald | M | 15 | 67.0 | 133.0 |
Thomas | M | 11 | 57.5 | 85.0 |
William | M | 15 | 66.5 | 112.0 |
Alfred | M | 14 | 69.0 | 112.5 |
Alice | F | 13 | 56.5 | 84.0 |
Barbara | F | 13 | 65.3 | 98.0 |
Carol | F | 14 | 62.8 | 102.5 |
Henry | M | 14 | 63.5 | 102.5 |
James | M | 12 | 57.3 | 83.0 |
Jane | F | 12 | 59.8 | 84.5 |
Janet | F | 15 | 62.5 | 112.5 |
Jeffrey | M | 13 | 62.5 | 84.0 |
John | M | 12 | 59.0 | 99.5 |
Joyce | F | 11 | 51.3 | 50.5 |
Judy | F | 14 | 64.3 | 90.0 |
Louise | F | 12 | 56.3 | 77.0 |
Mary | F | 15 | 66.5 | 112.0 |
Philip | M | 16 | 72.0 | 150.0 |
Robert | M | 12 | 64.8 | 128.0 |
Ronald | M | 15 | 67.0 | 133.0 |
Thomas | M | 11 | 57.5 | 85.0 |
William | M | 15 | 66.5 | 112.0 |
append with diff_varname |
Alfred | M | 14 | 69.0 | 112.5 | . |
Alice | F | 13 | 56.5 | 84.0 | . |
Barbara | F | 13 | 65.3 | 98.0 | . |
Carol | F | 14 | 62.8 | 102.5 | . |
Henry | M | 14 | 63.5 | 102.5 | . |
James | M | 12 | 57.3 | 83.0 | . |
Jane | F | 12 | 59.8 | 84.5 | . |
Janet | F | 15 | 62.5 | 112.5 | . |
Jeffrey | M | 13 | 62.5 | 84.0 | . |
John | M | 12 | 59.0 | 99.5 | . |
Joyce | F | 11 | 51.3 | 50.5 | . |
Judy | F | 14 | 64.3 | 90.0 | . |
Louise | F | 12 | 56.3 | 77.0 | . |
Mary | F | 15 | 66.5 | 112.0 | . |
Philip | M | 16 | 72.0 | 150.0 | . |
Robert | M | 12 | 64.8 | 128.0 | . |
Ronald | M | 15 | 67.0 | 133.0 | . |
Thomas | M | 11 | 57.5 | 85.0 | . |
William | M | 15 | 66.5 | 112.0 | . |
Alfred | M | 14 | . | 112.5 | 69.0 |
Alice | F | 13 | . | 84.0 | 56.5 |
Barbara | F | 13 | . | 98.0 | 65.3 |
Carol | F | 14 | . | 102.5 | 62.8 |
Henry | M | 14 | . | 102.5 | 63.5 |
James | M | 12 | . | 83.0 | 57.3 |
Jane | F | 12 | . | 84.5 | 59.8 |
Janet | F | 15 | . | 112.5 | 62.5 |
Jeffrey | M | 13 | . | 84.0 | 62.5 |
John | M | 12 | . | 99.5 | 59.0 |
Joyce | F | 11 | . | 50.5 | 51.3 |
Judy | F | 14 | . | 90.0 | 64.3 |
Louise | F | 12 | . | 77.0 | 56.3 |
Mary | F | 15 | . | 112.0 | 66.5 |
Philip | M | 16 | . | 150.0 | 72.0 |
Robert | M | 12 | . | 128.0 | 64.8 |
Ronald | M | 15 | . | 133.0 | 67.0 |
Thomas | M | 11 | . | 85.0 | 57.5 |
William | M | 15 | . | 112.0 | 66.5 |
Thanks so much!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.