I am trying to concatenate adult (A) and youth (Y) replicate weights together into a single column and there are 100 replicate weights of A and Y each. There are also four waves (W) of data, so I will end up creating a total of 400 concatenated variables.
I'm a beginner when it comes to arrays and loops, but I had written a macro to try to accomplish this. I realized the macro worked, but it kept over-writing itself so that only the last 100th replicate weight concatenation remained created in the new dataset. Does anyone how to write an array/loop/macro to make this work efficiently? Much appreciated.
%macro repweights (rep);
data All_Waves2; set All_Waves1;
W1_&rep=cats(R01_A_&rep, R01_Y_&rep);
W2_&rep=cats(R02_A_&rep, R02_Y_&rep);
W3_&rep=cats(R03_A_&rep, R03_Y_&rep);
W4_&rep=cats(R04_A_&rep, R04_Y_&rep);
%mend;
%repweights(1);
%repweights(2);
%repweights(3);
……
%repweights(100);
I was able to figure it out using arrays. Please note the original variable names were slightly abbreviated for simplicity in the original post and slightly differ from the variable names in this code below:
data All_Waves4; set All_Waves4a;
array adult1 (1:100) R01_A_PWGT1-R01_A_PWGT100;
array youth1 (1:100) R01_Y_PWGT1-R01_Y_PWGT100;
array Rep_W1_ (1:100);
do i=1 to 100;
Rep_W1_(i)=cats(adult1(i),youth1(i));
end; drop i;
array adult2 (1:100) R02_A_PWGT1-R02_A_PWGT100;
array youth2 (1:100) R02_Y_PWGT1-R02_Y_PWGT100;
array Rep_W2_ (1:100);
do i=1 to 100;
Rep_W2_(i)=cats(adult2(i),youth2(i));
end; drop i;
array adult3 (1:100) R03_A_AWGT1-R03_A_AWGT100;
array youth3 (1:100) R03_Y_AWGT1-R03_Y_AWGT100;
array Rep_W3_ (1:100);
do i=1 to 100;
Rep_W3_(i)=cats(adult3(i),youth3(i));
end; drop i;
array adult4 (1:100) R04_A_A01WGT1-R04_A_A01WGT100;
array youth4 (1:100) R04_Y_A01WGT1-R04_Y_A01WGT100;
array Rep_W4_ (1:100);
do i=1 to 100;
Rep_W4_(i)=cats(adult4(i),youth4(i));
end; drop i;
run;
You want to concatenate 100 values into a single text string? I urge you to reconsider this, as there are most definitely simpler ways to handle such data. And with simpler ways to handle such data, you also get the benefit of simpler coding.
Hi Paige,
Thanks for your help.
I am trying to concatenate two columns into a single column but I need to do this 400 times.
Wave 1 Adult 1 + Wave 1 Youth 1 = (concat Wave 1 Rep 1)
Wave 1 Adult 2 + Wave 1 Youth 2 = (concat Wave 1 Rep 2)
......
Wave 1 Adult 100 + Wave 1 Youth 100 = (concat Wave 1 Rep 100)
... and then repeat for Waves 2, 3, 4.
I was able to figure it out using arrays. Please note the original variable names were slightly abbreviated for simplicity in the original post and slightly differ from the variable names in this code below:
data All_Waves4; set All_Waves4a;
array adult1 (1:100) R01_A_PWGT1-R01_A_PWGT100;
array youth1 (1:100) R01_Y_PWGT1-R01_Y_PWGT100;
array Rep_W1_ (1:100);
do i=1 to 100;
Rep_W1_(i)=cats(adult1(i),youth1(i));
end; drop i;
array adult2 (1:100) R02_A_PWGT1-R02_A_PWGT100;
array youth2 (1:100) R02_Y_PWGT1-R02_Y_PWGT100;
array Rep_W2_ (1:100);
do i=1 to 100;
Rep_W2_(i)=cats(adult2(i),youth2(i));
end; drop i;
array adult3 (1:100) R03_A_AWGT1-R03_A_AWGT100;
array youth3 (1:100) R03_Y_AWGT1-R03_Y_AWGT100;
array Rep_W3_ (1:100);
do i=1 to 100;
Rep_W3_(i)=cats(adult3(i),youth3(i));
end; drop i;
array adult4 (1:100) R04_A_A01WGT1-R04_A_A01WGT100;
array youth4 (1:100) R04_Y_A01WGT1-R04_Y_A01WGT100;
array Rep_W4_ (1:100);
do i=1 to 100;
Rep_W4_(i)=cats(adult4(i),youth4(i));
end; drop i;
run;
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 16. 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.