Macro Understanding.

Solved
Frequent Contributor
Posts: 99

Macro Understanding.

Hi,can any one help to explain the following code within a macro;

%let Cond=;

%do i=1 %to &Nvars; *Nvars means how many variables within a dataset;
%if (&i>1) %then %let Cond=&Cond and ;
%if &&V&i=0 %then %do;
%if &&T&i=C %then   *Ti helps to identify the variable's type, c or n;
%let Cond=&Cond &&Var&i = '';   /* String C */
%if &&T&i=N %then
%let Cond=&Cond &&Var&i=. ;     /* Numeric N */
%end;
%else %do;
%if &&T&i=C %then
%let Cond=&Cond &&Var&i ne '' ;  /* String C */
%if &&T&i=N %then
%let Cond=&Cond &&Var&i ne . ;   /* Numeric N */
%end;
%end;

Pls give a hand. You need not explain sentance by sentance, what I don't understand is the changing of Cond,

what happened to the variable cond(condition)?

what's the value for cond after %let Cond=&Cond &&Var&i ne '' ;(say)

Very thanks.

Dawn

Accepted Solutions
Solution
‎06-07-2012 01:53 AM
Frequent Contributor
Posts: 95

Re: Macro Understanding.

I tried to replicate what your code was doing.

Added a %put statement before the last %end statement.

%put i=&i v&i = &&v&i t&i &&t&i Cond = "&Cond";

To me, it looks like a statement is being constructed to check whether the value of varıables are mıssıng or non-mıssıng based on the value of ındıcators and types of varıables.

Zafer

data test;

numvar = .; charvar = ''; output;

numvar = .; charvar = 'a'; output;

numvar = 1; charvar = ''; output;

numvar = 1; charvar = 'a'; output;

run;

data have;

set test;

v1 = (numvar = .);

v2 = (charvar = '');

keep v1 v2;

run;

%let NVars = 2;

%let var1 = numvar;

%let var2 = charvar;

%let t1 = N;

%let t2 = C;

%macro test(v1,v2);

%let Cond=;

%do i=1 %to &Nvars;

%if (&i>1) %then %let Cond=&Cond and ;

%if &&V&i=0 %then %do;

%if &&T&i=C %then

%let Cond=&Cond &&Var&i = ' ';   /* String C */

%if &&T&i=N %then

%let Cond=&Cond &&Var&i = . ;     /* Numeric N */

%end;

%else %do;

%if &&T&i=C %then

%let Cond=&Cond &&Var&i ne ' ' ;  /* String C */

%if &&T&i=N %then

%let Cond=&Cond &&Var&i ne . ;   /* Numeric N */

%end;

%put i=&i v&i = &&v&i t&i &&t&i Cond = "&Cond";

%end;

%mend;

%test(0,0);

%test(0,1);

%test(1,0);

%test(1,1);

All Replies
Frequent Contributor
Posts: 99

Re: Macro Understanding.

There is a dataset with 1/0 values only,(0 for missing value,1 for non-missing value)

observation 1 might be 1 1 1 1--- 1

this part of code is aiming to find whether the above variable pattern exist in the data set, using cond.

thanks.

Super Contributor
Posts: 350

Re: Macro Understanding.

Hi,

Use this options you will get lot of information in the log

Options symbolgen mprint mprintnest mlogic merror;

Thanks,

Shiva

Super User
Posts: 10,784

Re: Macro Understanding.

```what happened to the variable cond(condition)?
what's the value for cond after %let Cond=&Cond &&Var&i ne '' ;(say)
Very thanks.
Dawn
```

First of all, I don't think your code is good.

It seems that macro variable cond is trying to make a condition which can judge whether a variable(&Cond &&Var&i ) is missing.

Why not use missing() ,which can take care of both numberic and character variable.

You don't need to write so many and so obscure code.

Ksharp

Super User
Posts: 10,784

Re: Macro Understanding.

Plus,

Why not use PRX function to check this pattern, which is very very powerful.

```what's the value for cond after %let Cond=&Cond &&Var&i ne '' ;(say)
```

It will generate something like :

var1 ne && var2 && var3 && var4 ........

Solution
‎06-07-2012 01:53 AM
Frequent Contributor
Posts: 95

Re: Macro Understanding.

I tried to replicate what your code was doing.

Added a %put statement before the last %end statement.

%put i=&i v&i = &&v&i t&i &&t&i Cond = "&Cond";

To me, it looks like a statement is being constructed to check whether the value of varıables are mıssıng or non-mıssıng based on the value of ındıcators and types of varıables.

Zafer

data test;

numvar = .; charvar = ''; output;

numvar = .; charvar = 'a'; output;

numvar = 1; charvar = ''; output;

numvar = 1; charvar = 'a'; output;

run;

data have;

set test;

v1 = (numvar = .);

v2 = (charvar = '');

keep v1 v2;

run;

%let NVars = 2;

%let var1 = numvar;

%let var2 = charvar;

%let t1 = N;

%let t2 = C;

%macro test(v1,v2);

%let Cond=;

%do i=1 %to &Nvars;

%if (&i>1) %then %let Cond=&Cond and ;

%if &&V&i=0 %then %do;

%if &&T&i=C %then

%let Cond=&Cond &&Var&i = ' ';   /* String C */

%if &&T&i=N %then

%let Cond=&Cond &&Var&i = . ;     /* Numeric N */

%end;

%else %do;

%if &&T&i=C %then

%let Cond=&Cond &&Var&i ne ' ' ;  /* String C */

%if &&T&i=N %then

%let Cond=&Cond &&Var&i ne . ;   /* Numeric N */

%end;

%put i=&i v&i = &&v&i t&i &&t&i Cond = "&Cond";

%end;

%mend;

%test(0,0);

%test(0,1);

%test(1,0);

%test(1,1);

Frequent Contributor
Posts: 99

Re: Macro Understanding.

To Shivas,

Thanks for giving a debuging tools, I need time to digest.

To Ksharp,

Thanks for giving a thought, but I am a greenhand , have to learn before create.(those codes not mine).

To Alpay,

Thanks for taking time to write the sample code for me. Very useful. I am trying to reading the logs.

And thank you all for your help.

🔒 This topic is solved and locked.