簡體   English   中英

如何在 SAS Enterprise Guide 中使用循環創建多個宏變量?

[英]How to create multiple macro variables using a loop in SAS Enterprise Guide?

我一直在 SAS EG 中使用 SAS 數據集來創建宏變量,這些變量可用作從 SAS EG 到我的內部服務器的查詢中的變量。 宏變量的字符數限制為 65,534。 當我需要獲得長度為 9 到 15 位數字的 100k id 時,創建所需的宏變量的數量確實加起來了。 我在問社區是否有一種方法可以用循環而不是手動創建大量宏變量。

例如,創建這些宏變量的手動方法如下所示:

    proc sql; create table alerts as select distinct review_id format best12. from q4_21_alerts order by review_id;quit;
    proc sql; create table alerts1 as select review_id, monotonic() as number from alerts order by number; quit;

    proc sql; select distinct review_id into:alert_ids1 separated by ',' from alerts1 where number between 1 and 7000; quit;

    proc sql; select distinct review_id into:alert_ids2 separated by ',' from alerts1 where number between 7001 and 14000; quit;

    proc sql; select distinct review_id into:alert_ids3 separated by ',' from alerts1 where number between 14001 and 21000; quit;

    proc sql; select distinct review_id into:alert_ids4 separated by ',' from alerts1 where number between 21001 and 27000; quit;
    .
    .
    .
    proc sql; select distinct review_id into:alert_ids21 separated by ',' from alerts1 where number between 140001 and 147000; quit;

我希望找到一種方法來執行以下操作:

N = 145417 
#total number of review_ids that need to be contained in SAS macro variables
L = 8 
#length/number of characters/digits in each review_id
L = L + 1
#length/number of characters/digits in each review_id with 1 added for the comma separation in macro variable
stop = N*L

i = 1
while(i<=stop){
 some code to create all 21 macro variables 
}

然后留下宏變量 alert_ids1、alert_ids2、...、alert_ids21,其中包含我需要在查詢內部服務器時使用的所有 145,417 個 ID。

任何幫助,將不勝感激!

我使用過 google 和 sas 社區,並且有手動執行此過程的代碼......

我不確定您的最終查詢是什么,並建議構建一個專門過濾到您想要的 ID 的 SQL 查詢。 例如:

proc sql;
    create table want as
        select * 
        from have
        where id in(select id from id_table)
    ;
quit;

但是,如果您需要一個以逗號分隔的宏變量列表,並遵守 65,534 個字符的長度,最安全的方法是為每個宏變量創建一個 ID。 您可以使用數據步驟輕松完成此操作。

data _null_;
    set alerts1;
    
    call symputx(cats('alert_id', _N_), review_id); 
    call symputx('n_ids', _N_);
run;

這將創建宏變量:

alert_id1
alert_id2
alert_id3 
...

現在您需要創建一個循環,使這些都以逗號分隔。

%macro id_loop;
    %do i = 1 %to &n_ids;
&&alert_id&i %if(&i < &n_ids) %then %do;,%end;
    %end;
%mend;

請注意代碼格式有點奇怪,以保持輸出格式正確。 現在運行此宏,您將看到每個警報 ID 的逗號分隔列表:

%put %id_loop;

id1, id2, id3, ...

您可以將其放入查詢中,例如where alert_id in (%id_loop) 請記住,這樣做會加載帶有大量宏變量的符號表。 這不是推薦的查詢方式,但它是實現您所要求的一種方式。

使用數據步驟而不是 SQL 來創建宏變量。 您甚至可以創建第二個宏變量來引用所有生成的宏變量。

例如,假設您已確定始終可以將 1000 個值放入單個變量(數據步變量的限制是 32K 而不是宏變量的 64K 限制),那么您可以使用這樣的數據步:

data _null_;
   length string list $32767 ;
   group+1;
   do i=1 to 1000 until(eof);
     set alerts end=eof;
     string=catx(',',string,review_id);
   end;
   call symputx(cats('alert_id',group),string);
   list = catx(',',list,cats('&alert_id',group'));
   if eof then call symputx('alerts',list);
run;

現在您可以使用由字符串組成的單個宏變量 ALERTS

 &alert_id1,&alert_id2,....

在你的 SQL 代碼中:

 where review_id in (&alerts)

並過濾 ALERTS 數據集中的所有值,即使總字符串長於 64K 也是如此。 由於您將 1000 放入每個宏變量中,並且可以將對這些宏變量的大約 3000 個引用放入 ALERTS 宏變量中,因此您可以存儲多達 300 萬個值。

當然,您可能會遇到 SQL 處理器可以處理的限制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM