BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Stanley3
Obsidian | Level 7

Hi,

I'm new at macro writing and trying out what I thought should be an easy one. The purpose of the macro is to check that the categorical variables I created from continuous variables are set correctly. This is my code:

%macro check(data=, Cat=, Cont=);
    %let total_cat_vars = %sysfunc(countw(&Cat));
    %let total_cont_vars = %sysfunc(countw(&Cont));
    
    %do i = 1 %to &total_cat_vars;
        %let selected_cat_var = %scan(&Cat, &i);

        proc sort data=&data;
            by &selected_cat_var;
        run;

        %do j = 1 %to &total_cont_vars;
            %let selected_cont_var = %scan(&Cont, &j);

            proc means data=&data n nmiss min max mean median q1 q3;
                var &selected_cont_var;
                by &selected_cat_var;
            run;

        %end; 

    %end; 
%mend;

The problem is that it is running proc means for all of my continuous variables by each of my categorical variables (ie. I want only peso_nac to be ran with low_bw, but not sdg_nac). This can be seen here in the log with the mprint option:

Screenshot 2024-10-09 at 11.29.19 PM.png

I am not sure how to modify this macro to fix this. Any input is appreciated, thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Please show the code you used to actually CALL the macro.

Please show the SAS code you want it to produce for that call.

 

Do you even need a macro to do what you want?

Couldn't you just do it all with one PROC MEANS call?

proc means data=&data n nmiss min max mean median q1 q3;
  ways 1;
  class &cat;
  var &cont ;
run;

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

Please show the code you used to actually CALL the macro.

Please show the SAS code you want it to produce for that call.

 

Do you even need a macro to do what you want?

Couldn't you just do it all with one PROC MEANS call?

proc means data=&data n nmiss min max mean median q1 q3;
  ways 1;
  class &cat;
  var &cont ;
run;
PaigeMiller
Diamond | Level 26

Agreeing with @Tom 

Use one call to PROC MEANS for each data set.

--
Paige Miller
Stanley3
Obsidian | Level 7
Thank you!
Stanley3
Obsidian | Level 7
Thank you! This worked.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 993 views
  • 2 likes
  • 3 in conversation