2012-08-29 53 views
3

Supose我有一個宏變量,它是字創建列掃描的列表

%LET myvariable= Avocado Banana Rice Corn Mango Milk Strawberry Banana Banana Lime; 

現在的列表,我希望有一個數據集,其中每一行是該列表中的一個字,再加上一個ID號碼

ID Ingredient 
1  Avocado 
2  Banana 
3  Rice 
4  Corn 
5  Mango 
6  Milk 
7  Strawberry 
8  Banana 
9  Banana 
10 Lime 

我想是這樣的:

DATA food; 
DO id = 1 TO 10; 
Ingredient= SCAN(&myvariable.,id,' '); 
    OUTPUT; 
END; 
RUN; 

但是,這產生一個錯誤:「ERROR 388-185:期待算術運算符「。

這似乎是一件微不足道的事情,但不知何故,我卡住了。所以任何幫助將不勝感激。

一些背景資料:

在我的真實數據集,這個宏變量通過一個PROC SQL創建的,從一個數據集,其中每個條目有幾個單詞,用空格隔開。類似這樣的:

Product_ID Ingredients 
1    Sugar Milk Vanilla 
2    Corn Sugar Banana 
3    Apple Banana Maple_Syrup Oats 
...   ... 

回答

4

非常接近。如果沒有對評論爲什麼要做到這一點,你只需要把你的周圍宏變量名雙引號:

DATA food; 
    DO id = 1 TO 10; 
     Ingredient= SCAN("&myvariable.",id,' '); 
     OUTPUT; 
    END; 
RUN; 
3

無需宏觀變量和所有其他的東西。您可以直接從datastep執行此操作:

** 
** CREATE SOME SAMPLE DATA 
*; 
data sentences; 
    infile datalines truncover; 
    input Product_ID $ 
     Ingredients $100. 
     ; 
datalines; 
1 Sugar Milk Vanilla 
2 Corn Sugar Banana 
3 Apple Banana Maple_Syrup Oats 
; 
run; 


** 
** OUTPUT EACH WORD AS A ROW 
*; 
data words; 
    length word $100; 
    set sentences; 
    retain id 0; 

    cnt = 1; 
    word = scan(ingredients,cnt); 
    do while (word ne ''); 
    id = id + 1; 
    output; 
    cnt = cnt + 1; 
    word = scan(ingredients,cnt); 
    end; 

    drop ingredients cnt; 

run;