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

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,

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

 

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

4 REPLIES 4
Tom
Super User Tom
Super User

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
Alain38
Quartz | Level 8

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.

Tom
Super User Tom
Super User

 

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.

Alain38
Quartz | Level 8

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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