Konfu Chicken
Konfu Chicken

Reputation: 127

Combining datasets with renamed variables

I have a dataset which has answers to survey questions during two specific timelines. I want to join these datasets, but the variable names are the same. How can I change the variable names in both the datasets so that I can join them successfully?

ID Q1 Q2 Q3 Q4 etc...

I want to add the year '1995' to it so it becomes 1995Q1 199Q2 so on. I want to that for another dataset thatll say 1997.

data test;
rename q1 = 1995q1; * and so on this will continue *
set test.one;
run;
proc print data =test;
run;

But then itll take forever if i have more then 50 questions. Whats an efficient way to do this renaming method and then join the two datasets

Upvotes: 0

Views: 33

Answers (1)

Reeza
Reeza

Reputation: 21264

I would suggest stacking the data instead. This way it makes it easier to do the analysis in the long run. Make sure to add in a variable for year. This only makes sense if Q1 is the same across time.

data stacked;
   set y1995 (in=a)
       y2015 (in=b);
if a then year=1995; else year=2015;
run;

Otherwise you can rename using a variable list - assuming you have consistent names.

rename q1-q50 = Y1995_Q1 - Y1995_Q50;

Upvotes: 1

Related Questions