Search code examples
sasiterationradixdo-loops

Creating variables in do loop iteration SAS


I am new to SAS and i am trying to split columns and give the splitted results new names. What i hope to achieve: for example I have a column AV. My code needs to split it and calls the two new columns FROM_AV and TO_AV. I have tried multiple options, but it is still going wrong by creating the new names. i. It would be great if someone could help me out.

set work.transposed;
array aresplit AV TD ER PT;
do i=1 to 4;
FROM&aresplit[i]= scan(aresplit[i],1,',');
TO&aresplit[i]= scan(aresplit[i],2,','); 
end;
run;```

Solution

  • To make FROM_AV and TO_AV from AV you would use code like:

    FROM_AV = scan(AV,1,',');
    TO_AV = scan(AV,2,','); 
    

    If you want to replicate that for four variables using arrays then you need three arrays. One for input variable list and two for the output variable lists. (or the out array could be two dimensional).

    array in AV TD ER PT;
    array out1  FROM_AV FROM_TD FROM_ER FROM_PT;
    array out2  TO_AV TO_TD TO_ER TO_PT;
    do index=1 to dim(in);
       out1[index] = scan(in[index],1,',');
       out2[index] = scan(in[index],2,',');
    end;