DATA Step, Macro, Functions and more

resolving

Accepted Solution Solved
Reply
Contributor
Posts: 39
Accepted Solution

resolving

I have placed the names of all of the variables in a dataset into macro variables &&name&i.  through  &&name&n.

I have no trouble whatsoever with SAS resolving the macro reference in the code.

What is giving me a problem is the statemts in my data step which use the resolved macor variable.

For example:

1.

%do i=1 %to &n. %then %do;

    if &&name&i. not in (,.' ') then

             hold_&&name&i.=&&name&i.;

%end;

ERROR: All variables in array list must be the same type, i.e., all numeric or character.

2.

%do i=1 %to &n. %then %do;

%if &&name&i. not in(.," ")%then

    hold_&&name&i.=&&name&i

%end;

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable I resolves to 1

SYMBOLGEN: Macro variable NAME1 resolves to APPL_NBR

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: &&name&i. not in(.," ")

ERROR: The macro TESTIT will stop executing.

The problem ONLY lends itself to condition where if statments are employed.

The following works well and does generate any errors at all:

% do i=1 %to &n.;

    hold_&&name&=&&name&i.;

%end;


Accepted Solutions
Solution
‎10-06-2011 11:02 PM
Trusted Advisor
Posts: 1,301

resolving

you do loop syntax is not correct and your use of the in condition is incorrect.

%macro holdname(n);

  %do i=1 %to &n;

    if &&name&i not in ('.',',',' ') then hold_&&name&i=&&name&i;

  %end;

%mend;

%if does not have in condition available to it. (unless it was added in 9.3)

%macro holdname(n);

  %do i=1 %to &n;

    %if %quote(&&name&i) ne "," and %quote(&&name&i) ne "." and %quote(&&name&i) ne " " %then %let hold_&&name&i=&&name&i;

  %end;

%mend;

View solution in original post


All Replies
PROC Star
Posts: 7,473

resolving

Posted in reply to steve_citi

You'll have to show more of your code.  Is this part of a datastep?  If not, you are trying to create a variable outside of a data step and that will definitely give you problems.

In your first example, it simply isn't (I don't think) valid code:

%do i=1 %to &n. %then %do;


Solution
‎10-06-2011 11:02 PM
Trusted Advisor
Posts: 1,301

resolving

you do loop syntax is not correct and your use of the in condition is incorrect.

%macro holdname(n);

  %do i=1 %to &n;

    if &&name&i not in ('.',',',' ') then hold_&&name&i=&&name&i;

  %end;

%mend;

%if does not have in condition available to it. (unless it was added in 9.3)

%macro holdname(n);

  %do i=1 %to &n;

    %if %quote(&&name&i) ne "," and %quote(&&name&i) ne "." and %quote(&&name&i) ne " " %then %let hold_&&name&i=&&name&i;

  %end;

%mend;

PROC Star
Posts: 7,473

resolving

I'm still not sure what you are trying to do.  Does the following approximate it?

%let name1=foo;

%let name2=bar;

%let n=2;

data try;

  call missing(foo1);

  foo2=5;

  foo=3;

  bar1=1;

  call missing(bar2);

  bar=3;

run;

%macro testthis;

  data test;

    set try;

    %do i=1 %to &n.;

      if not missing(&&name&i.) then

       hold_&&name&i.=&&name&i.;

    %end;

  run;

%mend;

%testthis

Contributor
Posts: 39

resolving

You are both basically correct.

I want to loop around from i=1 to &n

and test each variable &&name1 through &&name&n to see if it is missing or blank. 

If it is not, place it in hold_&&name&i.

Now, I have modified the code to read:

%do i=1 to &n.;

if &&name&i. not  in ('.' , '') then

   hold_&&name&i.=&&name&i.

%end

AND IT WORKS!! it was simply an error in the "in" syntax.

Thanks everyone!

Trusted Advisor
Posts: 1,301

resolving

Posted in reply to steve_citi

One little side note, a macro variable name can only contain letters and numbers so any other character will automatically call the end of the name to resolve so the periods you are using are not necessary, so a macro variable name followed by a space, =, or ), etc does not need the . qualifier.  Maybe it is just a pet peeve of mine...

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 236 views
  • 3 likes
  • 3 in conversation