Macro/Array/Do loop q

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 131
Accepted Solution

Macro/Array/Do loop q

I created the following macro and am wondering what I can do (using another macro/arrays/do loop?) if I'd like to redo this data step n number of times where n is the number of covariates and for each n, the &covar. macro variable would change for each new covariate?

Should I clarify?

I appreciate the help in advance.

%macro meansd_estab (var, level1, level2, level3, level4, covar);

data &var._&covar.;

set tab_&var.;

    if &var.="&level1." & &covar._mean ne . then &covar._mean_int=&covar._mean;

    if &var.="&level1." & &covar._std ne . then &covar._std_int=&covar._std;

    &var._&level1.=&covar._mean_int||"("||&covar._std_int||")";

       

    if &var.="&level2." & &covar._mean ne . then &covar._mean_int=&covar._mean;

    if &var.="&level2." & &covar._std ne . then &covar._std_int=&covar._std;

    &var._&level2.=&covar._mean_int||"("||&covar._std_int||")";

       

    if &var.="&level3." & &covar._mean ne . then &covar._mean_int=&covar._mean;

    if &var.="&level3." & &covar._std ne . then &covar._std_int=&covar._std;

    &var._&level3.=&covar._mean_int||"("||&covar._std_int||")";

   

    if &var.="&level4." & &covar._mean ne . then &covar._mean_int=&covar._mean;

    if &var.="&level4." & &covar._std ne . then &covar._std_int=&covar._std;

    &var._&level4.=&covar._mean_int||"("||&covar._std_int||")";

   

run;

%mend;

%meansd_estab (var=estabgroup, level1=amp, level2=sst, level3=hih, level4=nur, covar=fns);


Accepted Solutions
Solution
‎04-16-2015 11:03 AM
Super User
Posts: 18,997

Re: Macro/Array/Do loop q

You can, look at the ways/type option in proc means. You also need to ignore the results window and look at the dataset you get - WANT, since that's what you'll have to work with.

View solution in original post


All Replies
Super User
Super User
Posts: 7,671

Re: Macro/Array/Do loop q

Yes, it looks like a good candidate for array processing.  Set your dataset up so;

VAR     LEVEL1     LEVEL2     LEVEL3     LEVEL4     LEVEL5      COVAR...

Then you can do:

array level{5};

array covar{..} covar...;

do i=1 to 5;

     if var=level{i} then covar{..}=...;

end;

The above code is vague as you haven't provided test data or required output.

Frequent Contributor
Posts: 131

Re: Macro/Array/Do loop q

Thanks, RW9!  To be clearer:

My input dataset for the DATA step is a proc tabulate output dataset in the following form:

                                                  

Obs   estabgroup      N     fns_Mean   fns_Std

   1      amp        46306      .         .          .

   2      hih        39270      .         .          .

   3      nur        11606      .         .          .

   4      sst         4840      .         .          .

   5                102022      .         .          .

   6      amp            .    40.8703     .        40.8703

   7      hih            .    44.0349     .        44.0349

   8      nur            .    82.7968     .        82.7968

   9      sst            .    82.7768     .        82.7768

  10                     .    48.5631     .          .

  11      amp            .      .       49.3312      .

  12      hih            .      .       51.7005      .

  13      nur            .      .       64.1790      .

  14      sst            .      .       68.1879      .

  15                     .      .       55.0111      .

Problem-solve1:

In the data step below I'm trying to create an analysis variable called estabgroup_&level._fns for each level/valid value of estabgroup (i.e. amp, hih, nur, and sst) which is the concatenation of the mean and std(for standard deviation) variable.


Problem-solve2:

After creating the mean/std analysis variable for each of the four levels of estabgroup (estabgroup_amp_fns, estabgroup_hih_fns, estabgroup_nur_fns, estabgroup_sst_fns,), I want to create mean/std variables at each of the four levels of estabgroup for n number of covariates.  Right now, I haven't introduced that I'll have n number of covariates; I've just let the macro variable covar=fns, the first covariate.

But currently my code (previous post) produces the following output, where mean and standard deviation aren't concatenated properly.

Problem-solve3:

How would I create the mean/std concatenated variable so that it displays as follows:  for each level of estabgroup, estab_amp/sst/nur/hih_fns=fns mean where level=amp/sst/nur/hih (fns std where level=amp/sst/nur/hih)?

Ideas?

Many thanks for your time and help in advance

Super User
Posts: 18,997

Re: Macro/Array/Do loop q

Use the output from proc means instead. With the STACKODS option it is much nicer to work with.

proc means data=have stackods nway n mean std;

class estabgroup;

var fns;

ods output summary=want;

run;

Frequent Contributor
Posts: 131

Re: Macro/Array/Do loop q

That's really helpful, Reeza-thank you!

When I code:

%let covar_list1 = fns parent_revenue firm_employees_parent1  ;   /*edit covar list here: parent_revenue firm_employees_parent1 N*/

proc means data=sample stackods nway n mean std;

class estabgroup ;

var &covar_list1.;

ods output summary=mean_estabgroup;

run;

I get :

Do you know if there is a way I can get the pooled mean for each covariate as well (instead of only the mean at each level of estabgroup?)

Thank you for your time and help

Solution
‎04-16-2015 11:03 AM
Super User
Posts: 18,997

Re: Macro/Array/Do loop q

You can, look at the ways/type option in proc means. You also need to ignore the results window and look at the dataset you get - WANT, since that's what you'll have to work with.

Frequent Contributor
Posts: 131

Re: Macro/Array/Do loop q

Hi Reeza--thanks!

I wanted to be able to update my list of covariates in just one place in my program (i.e. at the top where I definte the covar_list1 global macro var)

If I want to output each table of means and std deviations for each covariate crossed classed by estabgroup, however, do you know if I'm able to do this and if so how?

proc means data=sample stackods nway n mean std;

class estabgroup ;

var &covar_list1.;

ods output summary=mean_estabgroup;

run;

Thank you

Super User
Posts: 18,997

Re: Macro/Array/Do loop q

If you want a table for each variable in the list you'll need to loop through the list. It seem like it would be easier to generate all at once but it's your party.

Here's some code on how to loop through a macro variable list.

SAS - Macro Loop Through Variable Lists

🔒 This topic is solved and locked.

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

Discussion stats
  • 7 replies
  • 270 views
  • 6 likes
  • 3 in conversation