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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
FriedEgg
SAS Employee

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

5 REPLIES 5
art297
Opal | Level 21

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;


FriedEgg
SAS Employee

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;

art297
Opal | Level 21

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

steve_citi
Calcite | Level 5

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!

FriedEgg
SAS Employee

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

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
  • 5 replies
  • 1205 views
  • 3 likes
  • 3 in conversation