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

Good day everyone.

I am new to loops and macros.

 

So I have the task of running chi-square statistics on a series of sixteen variables

I feel I can get a more parsimonious SAS code by using loops and/or macros.

I have a series of variable X1 to X16 to run against y.

Please kindly assist.

Thanks

proc surveyfreq data=xx;
weight = wt;
strata = strata;
cluster = cluster;
tables y*x1 / chisq;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
proc surveyfreq data=xx;
weight = wt;
strata = strata;
cluster = cluster;
tables y*(x1-x16) / chisq;
run;

Does that give you what you want?

 


@oadeyemi wrote:

Good day everyone.

I am new to loops and macros.

 

So I have the task of running chi-square statistics on a series of sixteen variables

I feel I can get a more parsimonious SAS code by using loops and/or macros.

I have a series of variable X1 to X16 to run against y.

Please kindly assist.

Thanks

proc surveyfreq data=xx;
weight = wt;
strata = strata;
cluster = cluster;
tables y*x1 / chisq;
run;

 

View solution in original post

7 REPLIES 7
Reeza
Super User
proc surveyfreq data=xx;
weight = wt;
strata = strata;
cluster = cluster;
tables y*(x1-x16) / chisq;
run;

Does that give you what you want?

 


@oadeyemi wrote:

Good day everyone.

I am new to loops and macros.

 

So I have the task of running chi-square statistics on a series of sixteen variables

I feel I can get a more parsimonious SAS code by using loops and/or macros.

I have a series of variable X1 to X16 to run against y.

Please kindly assist.

Thanks

proc surveyfreq data=xx;
weight = wt;
strata = strata;
cluster = cluster;
tables y*x1 / chisq;
run;

 

oadeyemi
Obsidian | Level 7

Thank you for the response.

Yes, that can work too.

But x1 to x16 are long variable names.

I am thinking of pooling them in a local macro and use the macro for the step.

Do you understand my thought? Would it be more parsimonious?

 

Something like this....(note: this is not SAS format)

local a x1 x2 x3 x4 x5...x16

For each `var' in a....

....and I plug in a at 

tables y*`a'

 

Reeza
Super User

No, it would not be more parsimonious, in fact it's more verbose. 

 

Use Variable Lists of some form instead.

If you have a common prefix, or the variables are side by side this is easy. 

If not, and you list the variables in a local macro variable then you would just replace my x1-x16 with the macro variable instead. And that would still be more typing and characters so again, more verbose. The solution above is the most parsimonious solution, if that's your goal. 

 

Various methods to list variables are included here:

https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html

 

The method you've mentioned would not be parsimonious in either R or Python, there are easier ways in those languages as well. 

 


@oadeyemi wrote:

Thank you for the response.

Yes, that can work too.

But x1 to x16 are long variable names.

I am thinking of pooling them in a local macro and use the macro for the step.

Do you understand my thought? Would it be more parsimonious?

 

Something like this....(note: this is not SAS format)

local a x1 x2 x3 x4 x5...x16

For each `var' in a....

....and I plug in a at 

tables y*`a'

 


 

 

 

 

oadeyemi
Obsidian | Level 7

I agree with you.

Thanks for your explanation.

Thanks for sharing the link too.

PaigeMiller
Diamond | Level 26

@oadeyemi wrote:

Thank you for the response.

Yes, that can work too.

But x1 to x16 are long variable names.

I am thinking of pooling them in a local macro and use the macro for the step.

Do you understand my thought? Would it be more parsimonious?

 

Something like this....(note: this is not SAS format)

local a x1 x2 x3 x4 x5...x16

For each `var' in a....

....and I plug in a at 

tables y*`a'

 


Typing the list of variables into a macro variable, or typing the list of variables into the PROC SURVEYSELECT as suggested by @Reeza, is equal amount of effort.

 

Getting a macro to work that does the looping is extra work that doesn't exist if you use PROC SURVEYSELECT as Reeza showed. I don't see what you gain by a macro.

--
Paige Miller
oadeyemi
Obsidian | Level 7

I agree with you.

Thank you for your explanation.

The goal is to be as parsimonious as possible.

 

Watts
SAS Employee
proc surveyfreq data=xx;
    weight wt;
    strata strata;
    cluster cluster;
    tables y*(x1-x16) / chisq;
run;

Syntax correction. (Don't use "=" in WEIGHT, STRATA, and CLUSTER statements.)

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 7 replies
  • 1414 views
  • 3 likes
  • 4 in conversation