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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

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

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

lbarwick
Quartz | Level 8

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
data_null__
Jade | Level 19

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.

lbarwick
Quartz | Level 8

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

Ron_MacroMaven
Lapis Lazuli | Level 10

array charfield (*) &charfields:;

should be

array charfield (*) $ &charfields;

Ron_MacroMaven
Lapis Lazuli | Level 10

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

this replaces your call execute step;

see

Macro CallMacr - sasCommunity

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
  • 1125 views
  • 0 likes
  • 4 in conversation