简体   繁体   中英

How to pass a table as a parameter value in SAS for proc sql?

I'm rerunning a twenty line portion of code for multiple data sets where the only line that changes is the table name. Is there a way to parameterize the table name to pass into the code? I've found examples of passing a column as parameter but not an entire table.

Here's an example of what I'm doing where I would want to parameterize TABLE_NAME.

PROC SQL;
SELECT COUNT(number) AS band_1
FROM TABLE_NAME
WHERE VAR < 620

outer union

SELECT COUNT(number) AS band_2
FROM TABLE_NAME
WHERE VAR BETWEEN 620 AND 639;
RUN;

You can build a macro and provide a parameter, which will be a table name.

%macro union(table_name);
PROC SQL;
    SELECT COUNT(number) AS band_1
    FROM &TABLE_NAME
    WHERE VAR < 620

    outer union

    SELECT COUNT(number) AS band_2
    FROM &TABLE_NAME
    WHERE VAR BETWEEN 620 AND 639;
QUIT;
%mend union;

%union(a)
%union(b)
%union(c)

您可以使用动态查询,例如exec('select * from'+ @ tablename)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM