DATA Step, Macro, Functions and more

Macro interpreting array incorrectly

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

Macro interpreting array incorrectly

I am using call execute to pass values from a table to parameters in my macro code. The intent is to produce an output table for each prot and table containing only specific fields based on my projects meta data. One of the parameters is a list of fields that I have concatenated together that I want to use in a keep statement, e.g.

prottablechar_fieldsnum_fields
AAA0101CHAR1 CHAR2 CHAR3 CHAR4NUM1 NUM2 NUM3 NUM4
AAA0102CHAR1 CHAR2 CHAR3 CHAR4NUM1 NUM2 NUM3 NUM4
BBB0101CHAR1 CHAR2 CHAR3 CHAR4NUM1 NUM2 NUM3 NUM4
BBB0102CHAR1 CHAR2 CHAR3 CHAR4NUM1 NUM2 NUM3 NUM4

One variable in the source table is a list of character formatted fields, the other is a list of numeric formatted fields. I am passing the char_fields value to a character array within the macro, and the num_fields to a numeric array in the macro.

%macro freetext (prot,table,charfield,numfield);

data &prot._&table1

set &prot._&table;

by prot;

array charfield (*) &charfields:;

array numfield (*) &numfields:;

keep site prot patid &charfields &numfields; run;

%mend freetext

data _null_;

set source;

call

execute('%freetext('||prot||','||table||','||charfields||','||numfields||')');

run;

Now, this works for 99% of the tables produced via the macro, but for a few, the macro seems to be mis-interpreting the field type in the array statements and throws this error:

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

I have tried executing the data step outside of the macro by hard coding the array and I do not get this error message. Does anyone happen to know why the array is behaving differently within the macro vs. outside of it?


Accepted Solutions
Solution
‎06-02-2015 11:27 AM
Respected Advisor
Posts: 3,777

Re: Macro interpreting array incorrectly

The specification of the array variable list with the added colon : is probably not doing what you think and or what you want.

array charfield (*) &charfieldlist:;


If CHARFIELDLIST is CHAR1 CHAR3 then you are asking for variables CHAR1 and all variable that begin with CHAR3.  I think if you remove that colon your problem will be solved.

View solution in original post


All Replies
Super User
Super User
Posts: 7,392

Re: Macro interpreting array incorrectly

Hi,

Can you provide test data in a datastep which causes the issue, and the exact code you are using.  The code you posted is wrong in several ways:

%macro freetext (prot,table,charfield,numfield);

data &prot._&table1                    /* Missing semicolon to close data, and missing dot to finish macro variable */

set &prot._&table;                         / * we have no example of what AAA01.01 looks like, and datasets should not be numeric start */

by prot;

array charfield (*) &charfields:;        /* Macro variables are terminated by a full stop, not a semicolon.  Charfields is not a macro variable witihin this code &CHARFIELD. */

array numfield (*) &numfields:;       /* As above */

keep site prot patid &charfields &numfields; run;   /* as above.

%mend freetext

data _null_;

set source;

call

execute('%freetext('||prot||','||table||','||charfields||','||numfields||')');     /* Missing semicolon after first ) */

run;

Occasional Contributor
Posts: 14

Re: Macro interpreting array incorrectly

Sorry, in my haste to post this and anonymize my code there were probably some typos. Here is my complete code:

%macro freetext (prot,table,freefieldlist,charfieldlist,numfieldlist);

data &prot._&table;

set &prot..&table;

by prot;

        array charfield (*) &charfieldlist:;

        array numfield (*) &numfieldlist:;

    

    keep site prot patid &charfieldlist &numfieldlist; run;

%mend freetext;

data _null_;

set source;

call

execute('%freetext('||prot||','||table_name||','||freefieldlist||','||charfieldlist||','||numfieldlist||')');

run;

Here is an example of the source table I am using in call execute:

prottable_namecharfieldlistnumfieldlist
ABC01TAB1CHAR1 CHAR3 CHAR5NUM1 NUM3 NUM4
ABC02TAB2CHAR6 CHAR7 CHARNUM6 NUM7 NUM8
ABC03TAB3CHAR11 CHAR13 CHAR14NUM12 NUM13 NUM14

Here is an example of &prot..&table used in set statement (in this case the TAB1 table for prot ABC01):

siteprotpatidCHAR1CHAR2CHAR3CHAR4CHAR5NUM1NUM2NUM3NUM4NUM5
Site1ABC01001ABCDEFGHIJKLMNO111222333444555
Site2ABC01002AABDDEGGHJJKMMN112223334445556
Site3ABC01003BBCEEFHHIKKLNNO113224335446557

Here is an example of the &prot._&table output after the macro executes:

siteprotpatidchar1char3char5num1num3num4
Site1ABC01001ABCGHIMNO111333444
Site2ABC01002AABGGHMMN112334445
Site3ABC01003BBCHHIKKL113335446
Solution
‎06-02-2015 11:27 AM
Respected Advisor
Posts: 3,777

Re: Macro interpreting array incorrectly

The specification of the array variable list with the added colon : is probably not doing what you think and or what you want.

array charfield (*) &charfieldlist:;


If CHARFIELDLIST is CHAR1 CHAR3 then you are asking for variables CHAR1 and all variable that begin with CHAR3.  I think if you remove that colon your problem will be solved.

Occasional Contributor
Posts: 14

Re: Macro interpreting array incorrectly

This solved the issue, I probably copied this from another line somewhere and accidentally retained the colon.

Regular Contributor
Posts: 198

Re: Macro interpreting array incorrectly

array charfield (*) &charfields:;

should be

array charfield (*) $ &charfields;

Regular Contributor
Posts: 198

Re: Macro interpreting array incorrectly

... and, for a macro which calls a macro,

this replaces your call execute step;

see

Macro CallMacr - sasCommunity

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 260 views
  • 0 likes
  • 4 in conversation