简体   繁体   English

使用变量作为宏变量数据步骤

[英]use a variable as a macro variable data step

I'm looking for a way to use a normal variable value as a macro variable in a data step. 我正在寻找一种在数据步骤中将普通变量值用作宏变量的方法。

For example I have macro variable &statesList_Syphilis = AAA 例如我有宏变量&statesList_Syphilis = AAA
and another macro variable &statesList_Giardia = BBB 和另一个宏变量&statesList_Giardia = BBB

And in a data step I have a variable Germ wich contains 2 rows: "Syphilis" and "Giardia". 在数据步骤中,我有一个变量Germ,其中包含2行:“梅毒”和“贾第虫”。

In my data step I need to find AAA when iterating over the first row when Germ="Syphilis" 在我的数据步骤中,当Germ =“ Syphilis”遍历第一行时,我需要找到AAA
and BBB when iterating over the second row, when Germ="Giardia" 和Gerber =“ Giardia”遍历第二行时的BBB

an attempt would look like this 尝试看起来像这样

%let statesList_Syphilis = AAA;
%let statesList_Giardia = BBB;

data test;
    set mytablewithgerms; * contains variable Germ ;

    * use germ and store it in &germ macro variable ;
    * something like  %let germ = germ; or call symput ('germ',germ);

    * I want to be able to do this;
    xxx = "&&statesList_&germ"; * would give xxx = "AAA" or xxx = "BBB";

    * or this;
    &&statesList_&germ = "test"; * would give AAA = "test" or BBB = "test";

    run;

I don't think this is possible, but I figured I would ask just to be sure. 我认为这是不可能的,但我想我只是想确定一下。

Thanks! 谢谢!


EDIT (Following questions in the comments, I'm adding context to my specific problem, but I feel this is making things more complicated): 编辑(在评论中出现问题之后,我将上下文添加到我的特定问题中,但是我认为这会使事情变得更加复杂):

This was an attempt to simplify the problem. 这是试图简化问题的尝试。

In reality AAA and BBB are long lists of words like 实际上, AAABBB是一长串的单词,例如

"asymptomatic_1 fulminant_1 chronic_1 chronic_1 fatalFulminant_1 hepatocellular_1 compensated_1 hepatocellular_2 decompensated_1 fatalHepatocellular_1 fatalHepatocellular_2 fatalDecompensated_1"

And I don't want to store this long string in a variable, I want to iterate each word of this string in a do loop with something like: 而且我不想将此长字符串存储在变量中,我想使用类似以下方法在do循环中迭代此字符串的每个单词:

    %do k=1 %to %sysfunc(countw(&&statesList_&germ));
        %let state = %scan(&&statesList_&germ, &k);
        * some other code here ;
    %end;

EDIT2: 编辑2:
here is a more complete view of my problem: 这是我的问题的更完整视图:

%macro dummy();

data DALY1;
    * set lengths ;
    length Germ $10 Category1 $50 Category2 $50 AgeGroupDALY $10 Gender $2 value 8 stateList$999;

    * make link to hash table ;
    if _n_=1 then do;

        *modelvalues ----------------;
        declare hash h1(dataset:'modelData');
        h1.definekey ('Germ', 'Category1', 'Category2', 'AgeGroupDALY', 'Gender') ;
        h1.definedata('Value');
        h1.definedone();
        call missing(Germ, Value, Category1, Category2);
        * e.g.
          rc=h1.find(KEY:Germ, KEY:"ssssssssss", KEY:"ppppppppppp", KEY:AgeGroupDALY, KEY:Gender);

        *states ---------------------;
        declare hash h2(dataset:'states');
        h2.definekey ('Germ') ;
        h2.definedata('stateList');
        h2.definedone();

    end;

    set DALY_agregate;

    put "°°°°° _n_=" _n_;

    DALY=0; * addition of terms ;



    rc2=h2.find(KEY:Germ); * this creates the variable statesList;

    put "statesList =" statesList;

    * here i need statesList as a macro variable,;

    %do k=1 %to %sysfunc(countw(&statesList)); *e.g. acute_1 asymptomatic_1 ...;
        %let state = %scan(&statesList, &k);
        put "=== &k &state";
        &state = 1; * multiplication of terms ;

        * more code here;
    %end;


run;
%mend dummy;
%dummy;

EDIT3: 编辑3:
The input dataset looks like this 输入数据集如下所示

Germ    AgeGroup1 AgeGroup2 Gender Cases    Year
V_HBV   15-19   15-19   M   12  2015
V_HBV   15-19   15-19   M   8   2016
V_HBV   20-24   20-24   F   37  2011
V_HBV   20-24   20-24   F   46  2012
V_HBV   20-24   20-24   F   66  2013

The output dataset will add variables contained in the string defined by the macro variable which depends on the Germ. 输出数据集将添加包含在取决于Germ的宏变量定义的字符串中的变量。

eg for V_HBV it will create these variables: asymptomatic_1 fulminant_1 chronic_1 chronic_1 fatalFulminant_1 hepatocellular_1 compensated_1 hepatocellular_2 decompensated_1 fatalHepatocellular_1 fatalHepatocellular_2 fatalDecompensated_1 例如,对于V_HBV,它将创建以下变量:asymptomatic_1 fulminant_1慢性_1慢性_1 fatalFulminant_1肝细胞_1补偿_1肝细胞_2失代偿_1 fatalHepatocellular_1 fatalHepatocellular_2 fatalDecompensated_1

I'm not following the big picture, but one of the previous iterations of your question had some code (pseudo code) that illustrates possible confusion about how the macro language works. 我没有关注大局,但是您的问题的先前迭代之一包含一些代码(伪代码),这些代码说明了有关宏语言如何工作的可能的混淆。 Consider this step: 考虑以下步骤:

data _null_;
  germ="Syph";  
  call symput('germ',germ);
  %let Germ=%sysfunc(cats(germ));
  put "germ = &germ";
run;
%put &germ;

The log from executing that in a fresh SAS session shows: 在新的SAS会话中执行该日志的日志显示:

1    data _null_;
2      germ="Syph";
3      call symput('germ',germ);
4      %let Germ=%sysfunc(cats(germ));
5      put "germ = &germ";
6    run;

germ = germ

7    %put &germ;
Syph

Now let's talk about what's happening. 现在让我们谈谈正在发生的事情。 I'll use the line numbers from the log. 我将使用日志中的行号。

Line 2 assigns text string Syph to data step variable germ. 第2行将文本字符串Syph分配给数据步变量变量。 Nothing special. 没什么特别的。

Line 3 creates a macro variable named Germ, and assigns in the value of the datastep variable germ. 第3行创建一个名为Germ的宏变量,并分配datastep变量胚芽的值。 So it assigns it the value Syph. 因此,它为其分配值Syph。 This CALL SYMPUT statement executes when the data step executes. 该CALL SYMPUT语句在执行数据步骤时执行。

Line 4 is a macro %let statement. 第4行是宏%let语句。 It creates a macro variable named Germ, and assigns it the value germ. 它创建一个名为Germ的宏变量,并为其赋值值胚芽。 Because this is a macro statement, it executes before any of the DATA STEP code has executed. 由于这是一个宏语句,因此它将在执行任何DATA STEP代码之前执行。 It does not know about data step variables. 它不知道数据步骤变量。 Line 4 is equivalent to %let Germ=germ. 第4行等效于%let Germ = germ。 To the macro language, the right hand side is just a four-character string germ. 对于宏语言,右侧只是一个四个字符的字符串。 It is not the name of a data step variable. 它不是数据步骤变量的名称。 %syfunc(cats()) is doing nothing, because there is no list of items to concatenate. %syfunc(cats())什么也不做,因为没有要连接的项目列表。

Line 5 is a data step PUT statement. 第5行是数据步骤PUT语句。 The macro reference &germ is resolved while the data step is compiling. 在编译数据步骤时,将解析宏引用&germ。 At this point the macro variable germ resolves to Germ because the %LET statement has executed (the CALL SYMPUT statement has not executed yet). 此时,由于%LET语句已执行(CALL SYMPUT语句尚未执行),因此宏变量胚解析为Germ。

Line 7 is a %PUT statement that executes after the DATA NULL step has completed (and after the CALL SYMPUT has written the value Syph to macro variable Germ). 第7行是%PUT语句,该语句在DATA NULL步骤完成后(以及CALL SYMPUT将值Syph写入宏变量Germ之后)执行。

As a general principle, it is difficult (and unusual) to have a single data step in which you are using data to create a macro variable (eg via call symput) and using that macro variable in the same step (ie referencing the macro variable). 作为一般原则,只有一个数据步骤很困难(而且很不寻常),在该步骤中,您正在使用数据来创建宏变量(例如,通过调用symput)并在同一步骤中使用该宏变量(即,引用宏变量) )。 Macro references are resolved before any of the data step code executes. 宏引用在任何数据步骤代码执行之前就已解析。

Typically if your data are already in a dataset, you can get what you want with data step statements (DO loops rather than %DO loops, etc). 通常,如果数据已经在数据集中,则可以使用数据步骤语句(DO循环而不是%DO循环等)获得所需的内容。 Or alternatively you can use one DATA step to generate your macro variables, and a second DATA step can reference them. 或者,您可以使用一个DATA步骤来生成宏变量,而第二个DATA步骤可以引用它们。

Hope that helps. 希望能有所帮助。

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

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