简体   繁体   English

使用SAS宏变量在PROC SQL中创建变量名称

[英]Use SAS macro variable to create variable name in PROC SQL

I'm trying to create a set of flags based off of a column of character strings in a data set. 我正在尝试根据数据集中的一列字符串创建一组标志。 The string has thousands of unique values, but I want to create flags for only a small subset (say 10). 该字符串具有数千个唯一值,但我只想为一小部分子集(例如10)创建标志。 I'd like to use a SAS macro variable to do this. 我想使用SAS宏变量来执行此操作。 I've tried many different approaches, none of which have worked. 我尝试了许多不同的方法,但都没有奏效。 Here is the code that seems simplest and most logical to me, although it's still not working: 这对我来说似乎是最简单,最合乎逻辑的代码,尽管仍然无法正常工作:

%let Px1='12345'; 

PROC SQL;

CREATE TABLE CLAIM1 AS
SELECT 

b.MEMBERID
, b.ENROL_MN
, CASE WHEN (a.PROCEDURE = &Px1.) THEN 1 ELSE 0 END AS CPT_+&Px1.  
, a.DX1
, a.DX2
, a.DX3
, a.DX4

FROM ENROLLMENT as b
left join CLAIMS as a
on a.MEMBERID = b.MEMBERID;

QUIT;

Obviously there is only one flag in this code, but once I figure it out the idea is that I would add additional macro variables and flags. 显然,这段代码中只有一个标志,但是一旦我弄清楚了,我的想法是我将添加其他宏变量和标志。 Here is the error message I get: 这是我收到的错误消息:

8048  , CASE WHEN (PROCEDURE= &Px1.) THEN 1 ELSE 0 END AS CPT_+&Px1.
                                                           -
                                                           78
ERROR 78-322: Expecting a ','.

It seems that the cause of the problem is related to combining the string CPT_ with the macro variable. 看来问题的原因与将字符串CPT_与宏变量组合在一起有关。 As I mentioned, I've tried several approaches to addressing this, but none have worked. 正如我提到的,我已经尝试了几种方法来解决此问题,但是没有一种方法可行。

Thanks in advance for your help. 在此先感谢您的帮助。

Something like this normally requires dynamic sql (although I am not sure how will that works with SAS, I believe it may depend on how you have established connection with the database). 像这样的事情通常需要动态sql(尽管我不确定如何在SAS中工作,但我相信它可能取决于您与数据库建立连接的方式)。

Proc sql;

DECLARE @px1 varchar(20) = '12345'
       ,@sql varhcar(max) = 
               'SELECT b.MEMBERID
                     , b.ENROL_MN
                     , CASE WHEN (a.PROCEDURE = ' + @Px1 + ') THEN 1 ELSE 0 
                                  END AS CPT_' + @px1  + '
                     , a.DX1
                     , a.DX2
                     , a.DX3
                     , a.DX4

                   FROM ENROLLMENT as b
                   left join CLAIMS as a
                   on a.MEMBERID = b.MEMBERID'

EXEC sp_excutesql @sql;



QUIT;

Your issue here is the quotes in the macro variable. 您的问题是宏变量中的引号。

%let Px1='12345';

So now SAS is seeing this: 因此,SAS现在看到了这一点:

... THEN 1 ELSE 0 END AS CPT_+'12345'

That's not remotely legal! 那是不合法的! You need to remove the ' . 您需要删除'

%let Px1 = 12345;

Then add back on at the right spot. 然后在正确的位置重新添加。

CASE WHEN a.procedure = "&px1." THEN 1 ELSE 0 END AS CPT_&px1.

Note " not ' as that lets the macro variable resolve. 注意" not '因为这样可以使宏变量解析。

If you have a list it might help to put the list into a table. 如果有列表,将列表放入表可能会有所帮助。 Then you can use SAS code to generate the code to make the flag variables instead of macro code. 然后,您可以使用SAS代码生成代码以使标志变量代替宏代码。

Say a table with PX code variable. 说一个带有PX代码变量的表。

data pxlist;
  input px $10. ;
cards;
12345 
4567
; 

You could then use PROC SQL query to generate code to make the flag variable into a macro variable. 然后,您可以使用PROC SQL查询生成代码,以将标志变量变为宏变量。

proc sql noprint;
select catx(' ','PROCEDURE=',quote(trim(px)),'as',cats('CPT_',px))
  into :flags separated by ','
  from pxlist
;
%put &=flags;
quit;

Code looks like 代码看起来像

PROCEDURE= "12345" as CPT_12345,PROCEDURE= "4567" as CPT_4567

So if we make some dummy data. 因此,如果我们做一些虚拟数据。

data enrollment ;
  length memberid $8 enrol_mn $6 ;
  input memberid enrol_nm;
cards;
1 201612
;
data claims;
  length memberid $8 procedure $10 dx1-dx4 $10 ;
  input memberid--dx4 ;
cards;
1 12345 1 2 . . . 
1 345 1 2 3 . .
;

We can then combine the two tables and create the flag variables. 然后,我们可以组合两个表并创建标志变量。

proc sql noprint;
create table want as
  select *,&flags
  from ENROLLMENT 
  natural join CLAIMS
;
quit;

Results 结果

memberid procedure dx1 dx2 dx3 dx4 enrol_mn CPT_12345 CPT_4567
1        12345     1   2           201612   1         0
1        345       1   2   3       201612   0         0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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