Question

I've got the following issue and I'm not sure on how to do this.

I'm trying to merge 6000 variables through the code below

Please find below the piece of code I've written for two of the variables

data big_aat_1; 
merge Aat_1(rename=(var14=var14_t0  var28=var28_t_0)) 
      Aat_2(rename=(var14=var14_t_1 var28=var28_t_1)) 
      Aat_3(rename=(var14=var14_t_2 var28=var28_t_2)) 
      Aat_4(rename=(var14=var14_t_3 var28=var28_t_3)) 
      Aat_5(rename=(var14=var14_t_4 var28=var28_t_4))
      Aat_6(rename=(var14=var14_t_5 var28=var28_t_5)); 
by nouv_date;
run;

My aim is to try to automate my piece of code for the 6000 variables I have and keep the way I'm doing it e.g. with the merge.

The result will all the variables would be like the one below. The ...represent the rest of the variables

data big_aat_1; 
merge Aat_1(rename=(var14=var14_t0 var28=var28_t_0 var37=var37_t_0 ...)) 
      Aat_2(rename=(var14=var14_t_1 var28=var28_t_1 var37=var37_t_1 ...)) 
      Aat_3(rename=(var14=var14_t_2 var28=var28_t_2 var37=var37_t_2 ...)) 
      Aat_4(rename=(var14=var14_t_3 var28=var28_t_3 var37=var37_t_3 ...)) 
      Aat_5(rename=(var14=var14_t_4 var28=var28_t_4 var37=var37_t_4 ...))
      Aat_6(rename=(var14=var14_t_5 var28=var28_t_5 var37=var37_t_5 ...)); 
by nouv_date;
run;

There are 2 things I need to state

1) I have a dataset / table that contains all the distinct variable names (e.g. var14, var28 ...). It would be great if I can use it. The name of the dataset is dicoAg

2) I need to keep the merge for some reasons I cannot talk about here.

If you have any insight

Was it helpful?

Solution

I started creating test data sets (you obviously already have them):

%MACRO P;
%DO I=1 %TO 6;
data aat_&I;
    %DO J=1 %TO 6000;
        var&J=&J;
    %END;
    nouv_date=1;output;
run;
%END;
%MEND;
%P;

and then I used proc contents to have a list of the variables (you can skip this step and use dicoAg):

proc contents data=aat_1 varnum out=vars;run;

and then you have sas write the rename code for you:

data _NULL_;                                    
    set vars /*dicoAg*/(where=(NAME^="nouv_date")) end=fine;
    file "MyPath\Rename.sas"; 
    if _N_=1 then do;       
        put '%MACRO RENAME(J=);   ';                             
        put '(rename=(         ';    
    end; 
    /*intead of NAME use the variable in dicoAg which contains all the variables' names*/ 
    put '           ' NAME '=' NAME +(-1) '_&J';  
    if fine then do;                      
        put '      ))            ';                        
        put '%MEND;           ';                          
    end;
run;   

you include the code:

%include "MyPath\Rename.sas";

and at the end you write the macro to do the merge:

%MACRO P;
data big_aat_1; 
    merge
        %DO D=1 %TO 6;
            aat_&D. %RENAME(J=&D)
        %END;
    ;
    by nouv_date;
run;
%MEND;
%P;

OTHER TIPS

Everyone,

Without going into full details, my man-a and I did that

data big_aat_1;
merge %do j=1 %to 6 ; Aat_&j(rename=(%do i=1 %to &&&&nvar&&pays&l  ; &&&&var&&pays&l.._&i=&&&&var&&pays&l.._&i.._t%eval(&j-1) %end ; )) %end ; ;
by nouv_date;
run;

Not perfect nor superbly efficient but doing the trick.

Explanation :

&&&&nvar&&pays&lis the max number of variables &&&&var&&pays&l.._&iis the variable

The results will give you something like this

 merge Aat_1(rename=( var1=var1_t0 var31=var31_t0 var60=var60_t0 var90=var90_t0 var119=var119_t0 ...

Aat_6(rename=( var1=var1_t5 var31=var31_t5 var60=var60_t5 var90=var90_t5 var119=var119_t5...

Best.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top