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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Alpay
Fluorite | Level 6

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);

View solution in original post

6 REPLIES 6
bbb_NG
Fluorite | Level 6

Add something.

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.

shivas
Pyrite | Level 9

Hi,

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

Options symbolgen mprint mprintnest mlogic merror;

Thanks,

Shiva

Ksharp
Super User

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

Ksharp
Super User

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 ........

Alpay
Fluorite | Level 6

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);

bbb_NG
Fluorite | Level 6

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1102 views
  • 6 likes
  • 4 in conversation