Solved
Contributor
Posts: 37

# Need help to understand macro! If possible please kindly explain me.

I need to create a macro. I have dataset having variables from v1 to v7 (independent variables) and y for response. the number of predictors using "varnum" for execution.

Accepted Solutions
Solution
‎04-14-2016 09:39 PM
Super User
Posts: 10,209

## Re: Need help to understand macro! If possible please kindly explain me.

Very simple, but may lead you in the right direction:

``````%macro regressions (varnum);
%do i = 1 %to &varnum;
proc reg;
model y=v&i;
run;
%end;
%mend;``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

All Replies
Solution
‎04-14-2016 09:39 PM
Super User
Posts: 10,209

## Re: Need help to understand macro! If possible please kindly explain me.

Very simple, but may lead you in the right direction:

``````%macro regressions (varnum);
%do i = 1 %to &varnum;
proc reg;
model y=v&i;
run;
%end;
%mend;``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 37

## Re: Need help to understand macro! If possible please kindly explain me.

[ Edited ]

%macro regressions (varnum);
%do i = 1 %to &varnum;
proc reg data=new;
model y=v&i;
run;
proc gplot data=new;
plot y*i/vaxis= 0 to 1 by 0.1;
run;
%end;
%mend;
%regressions(5)

I got it for regression. But how can I use proc gplot ? It does not work with macro. It shows that at least one plot or bubble statement must be given.

Posts: 1,242

## Re: Need help to understand macro! If possible please kindly explain me.

Hi @Cathy,

The plot request should read y*v&i rather than y*i.

Contributor
Posts: 37

Thank you.

Contributor
Posts: 37

## Re: Need help to understand macro! If possible please kindly explain me.

[ Edited ]

I have a question regarding using this code;

for example if I want to create start  at 3 and stop at 5.

%macro questions(start=3,stop=5);
%do i = &start %to &stop;
proc reg data=new;
model y=v&i;
run;
%do j=&i+1 %to &stop+1;
proc gplot data=new;
plot v&i*v&j;
run;
%end;
%end;
%mend;
%questions(3)

Contributor
Posts: 37

## Re: Need help to understand macro! If possible please kindly explain me.

I got this...%questions((start=3,stop=5)

it print out...finally
Contributor
Posts: 37

## Re: Need help to understand macro! If possible please kindly explain me.

Thanks a lot again. This helps me a lot too
Contributor
Posts: 37

## Re: Need help to understand macro! If possible please kindly explain me.

Thanks a lot, I got it finally and giving me a good direction.
Super User
Posts: 9,599

## Re: Need help to understand macro! If possible please kindly explain me.

Hi,

Well, your problem seems to lie in the way you have set your data up - i.e. wide rather than long.  I am not familiar with proc reg, so am guessing a bit on syntaxt here, but all procedures are built to allow by group processing - which is both faster, and more efficient, plus easier to maintain.  To use this however your data needs to be long data rather than wide.  Don't confuse what data you may use in an output review file (which is likely to need transposing) with data you use for your programming, the two can be totally different.  An example below shows how to normalise your data, and then use a by grou - see how much simpler the code is, easier to maintain, no macro at all just plain base SAS - note that you have not provided any test data (in the form of a datastep) so this is just an example:

```proc transpose data=have out=t_have;
by <id variables>;  /* Replace with your id variables */
var v:;
run;
proc reg data=t_have;
by _name_;
model y=col1;
run;```
Contributor
Posts: 37

## Re: Need help to understand macro! If possible please kindly explain me.

thank you, but I need to use macro for this.

Super User
Posts: 9,599

## Re: Need help to understand macro! If possible please kindly explain me.

Why?  There is never a "need" to use any tool, use the one thats fits the job best?  Macro language is designed as a code generation toolkit, nothing more, Base SAS is the programming language.

Contributor
Posts: 37

## Re: Need help to understand macro! If possible please kindly explain me.

[ Edited ]

I am studying little SAS book and preparing to get certificate too. That's why I need to know macro too. Thank you.

Contributor
Posts: 37

## Re: Need help to understand macro! If possible please kindly explain me.

Thank you so much too.
Posts: 1,270

## Re: Need help to understand macro! If possible please kindly explain me.

[ Edited ]

If you are looking for predictors contributing more in the model then you can try selection criteria based on rsquare to flag predictors for the final model:

proc reg data=have;
model y=v:/selection=rsquare;
plot r.*(p. v: );
plot student.*obs. / vref=3 2 -2 -3;
run;
quit;

🔒 This topic is solved and locked.