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

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);

1 ACCEPTED SOLUTION

Accepted Solutions
kai_cody
Fluorite | Level 6

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;

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
kai_cody
Fluorite | Level 6

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. 

kai_cody
Fluorite | Level 6

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1682 views
  • 1 like
  • 2 in conversation