DATA Step, Macro, Functions and more

create a loop

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

create a loop

Hi,

I'm new to SAS and I would like to create a loop from 1 to 48. Column _1_i is projected onto
columns _2_i, _3_i and _4_i using Generalized Least Squares method. So there are 48 regressions to perform.
Coefficients (Estimate) and standard-errors (StdErr) are extracted in a dataset called "Coeff".
I wrote an example, without loop, with just the 2 first regressions:

        proc glm data=Sample;
        model _1_1 = _2_1 _3_1 _4_1;
        ods output ParameterEstimates=Coeff;
        quit;

        proc glm data=Sample;
        model _1_2 = _2_2 _3_2 _4_2;
        ods output ParameterEstimates=Coeff2;
        quit;

        /* Add new obs to original data set */
        proc append base=Coeff data=Coeff2;
        run;

        /* Delete the temp data set */
        proc delete data= Coeff2;
        run;

I tried to create a macro with "%do i=1 %to 48" and "model &_1_i. = &_2_i. &_3_i. &_4_i.;" but I don't manage to make it work, notably because of the temporary datasets supposed to be named Coeff2, ..., to Coeff48. I use them to add results in the existing dataset "Coeff", and then delete them (only way I found to add new results without overwriting).

Thank you in advance so much for your help,


Accepted Solutions
Solution
‎05-14-2016 02:11 PM
Super User
Super User
Posts: 6,500

Re: create a loop

 

run it with N=2 or 3 and test.

 

In general if want to repeat a step that generates a data set and combine them you could use proc append.  You would want to do something like this in the macro.

 

.... out=coeff&i ....

proc append base=all_coeff data=coeff&i force ;
run;

Then before you call the macro you might add this to make sure that you don't have an old version of your base dataset lying around.

proc delete data=all_coeff;
run;

%mymacro(runs=48);

Note that using PROC APPEND requires that the datasets have the same variables.  If the datasets might have different variables you might need to use a data step instead so that you can incorporate new variables as they are generated.

View solution in original post


All Replies
Super User
Super User
Posts: 6,500

Re: create a loop

Looks like you are not quite getting the syntax for how to use the value of the macro variable to generate names.  Essentially SAS will just replace the reference to the macro variable with text that it contains.

 

So if you want generate the variable name pattern of _1_1, _1_2, _1_3, ...  then you use

_1_&i

You might also need to watch out if the variable part is in the middle of a string.  In that case you need to add a period after the name of the macro variable so that SAS can tell where the name ends and the normal text begins again.

 

_var&i._with_suffix
Contributor
Posts: 20

Re: create a loop

Thank you so much for your quick reply. Is the syntax the same to name the output datasets? i.e.:

"ods output ParameterEstimates=Coeff&i"

since I will have 48 output datasets (Coeff, Coeff2, ..., Coeff48), including 47 which are temporary and that I will delete after merging with the first one.

Solution
‎05-14-2016 02:11 PM
Super User
Super User
Posts: 6,500

Re: create a loop

 

run it with N=2 or 3 and test.

 

In general if want to repeat a step that generates a data set and combine them you could use proc append.  You would want to do something like this in the macro.

 

.... out=coeff&i ....

proc append base=all_coeff data=coeff&i force ;
run;

Then before you call the macro you might add this to make sure that you don't have an old version of your base dataset lying around.

proc delete data=all_coeff;
run;

%mymacro(runs=48);

Note that using PROC APPEND requires that the datasets have the same variables.  If the datasets might have different variables you might need to use a data step instead so that you can incorporate new variables as they are generated.

Contributor
Posts: 20

Re: create a loop

Thank you so much Tom! It works! Smiley Happy My final code is:

 

%macro test;
     %do i=1 %to 48;

proc glm data=GLS.Sample;
    model _1_&i. = _2_&i. _3_&i. _4_&i;
    ods output ParameterEstimates=GLS.Coeff&i;
quit;

proc append base=GLS.All_Coeff data=GLS.Coeff&i. force;
run;

proc delete data=GLS.Coeff&i;
run;

    %end;
%mend test;
%test(runs=48);

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 368 views
  • 2 likes
  • 2 in conversation