2016-11-09 107 views
1

我有一個奇怪的問題與PROC意義和使用StackODSOutput選項。考慮這個例子。SAS PROC與StackODSOutput的意義:一個名爲「標籤」的變量

我首先要建立一個分析數據集假人。

/* Step-1: Create a dummy dataset for analysis */ 
data ds1; 
    label x = 'Variable X'; 
    label y = 'Variable Y'; 
    do i = 1 to 100; 
     x = ranuni(1234); 
     y = ranuni(5678); 
     keep x y; 
     output; 
    end; 
run; 

然後,我用StackODSOutput選項運行PROC MEANS。這將創建一個名爲「stats」的輸出數據集。

/* Step-2: I run PROC means to capture the output in a dataset called stats */ 
proc means data=ds1 StackODSOutput mean; 
    var x y; 
    ods output summary=stats; 
run; 

這個「stats」數據集有一個名爲「Label」的變量。我知道這個變量是存在的,因爲我做了一個proc內容,並且在那裏看到這個變量。

/* Step-3: Confirm visually that there is a variable called Label in stats dataset */ 
proc contents data=stats varnum; run; 

但是,我似乎無法在任何地方引用這個名爲「Label」的變量。例如,以下PROC SQL語句會生成一個錯誤。我可以在「統計」數據集中引用所有其他變量,而不會有任何問題。

/* Step-4: But, I cannot seem to reference the variable called "Label" in stats dataset! */ 
proc sql; 
    select Variable, Label from stats; 
quit; 

的錯誤如下:

43   proc sql; 
44   select Variable, Label from stats; 
ERROR: The following columns were not found in the contributing tables: Label. 
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements. 
45   quit; 

你知道,如果我做錯了什麼?我的SAS代碼或SAS安裝有問題嗎?

MY SAS版本的SAS 9.3(9.03.01M2P08152012)。

謝謝。

Karthik。

根據Reeza的要求,這裏是完整的日誌輸出。

1               The SAS System       15:52 Wednesday, November 9, 2016 

1   %_eg_hidenotesandsource; 
5   %_eg_hidenotesandsource; 
20   
21   /* Step-1: Create a dummy dataset for analysis */ 


22   data ds1; 
23   label x = 'Variable X'; 
24   label y = 'Variable Y'; 
25   do i = 1 to 100; 
26    x = ranuni(1234); 
27    y = ranuni(5678); 
28    keep x y; 
29    output; 
30   end; 
31   run; 

NOTE: The data set WORK.DS1 has 100 observations and 2 variables. 
NOTE: DATA statement used (Total process time): 
     real time   0.01 seconds 
     cpu time   0.01 seconds 


32   
33   /* Step-2: I run PROC means to capture the output in a dataset called stats */ 
34   proc means data=ds1 StackODSOutput mean; 
35   var x y; 
36   ods output summary=stats; 
37   run; 

NOTE: The data set WORK.STATS has 2 observations and 3 variables. 
NOTE: There were 100 observations read from the data set WORK.DS1. 
NOTE: PROCEDURE MEANS used (Total process time): 
     real time   0.06 seconds 
     cpu time   0.03 seconds 


38   
39   /* Step-3: Confirm visually that there is a variable called Label in stats dataset */ 
40   proc contents data=stats varnum; run; 

NOTE: PROCEDURE CONTENTS used (Total process time): 
     real time   0.03 seconds 
     cpu time   0.03 seconds 


41   
42   /* Step-4: But, I cannot seem to reference the variable called "Label" in stats dataset! */ 
43   proc sql; 
44   select Variable, Label from stats; 
ERROR: The following columns were not found in the contributing tables: Label. 
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements. 
45   quit; 
NOTE: The SAS System stopped processing this step because of errors. 
NOTE: PROCEDURE SQL used (Total process time): 
     real time   0.00 seconds 
     cpu time   0.00 seconds 

2               The SAS System       15:52 Wednesday, November 9, 2016 

46   /* What! */ 
47   
48   
49   
50   %_eg_hidenotesandsource; 
62   
63   
64   %_eg_hidenotesandsource; 
67   
+0

代碼工作正常,我。立即運行並再次檢查,可能你在某處忘記了某處。你使用什麼版本的SAS?我在9.4 TS1M3上。如果仍然無法運行,請在一次運行中從運行代碼發佈完整日誌。 – Reeza

+0

我的SAS版本是SAS 9.3(9.03.01M2P08152012)。另外,我添加了完整的SAS日誌。 – Karthik

+0

嘗試設置'option validvarname = v7;'在開始時再試一次。如果不行,請發佈proc內容的輸出。我還建議提高技術支持。如果知道的問題,他們可以指出你在正確的方向。或者至少可以跟蹤它。 – Reeza

回答

1

這是一個錯誤。它會創建一個尾隨空格的變量名,如果validvarname被設置爲ANY,則可以在EG中使用該名稱。

修復:

Option validvarname=V7; 

https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-EG-won-t-recognize-a-variable-that-has-the-name-quot-Label/m-p/294936

+0

這解決了這個問題。基本上,我必須添加「Option validvarname = V7;」聲明,然後再調用PROC MEANS並將其恢復爲默認的「Option validvarname = any;」之後。謝謝! – Karthik

1

我在運行代碼時遇到同樣的問題。我有SAS 9.4並且正在Linux上運行。這裏是我的問題的評價,在我結束:

data _NULL_; 
    set stats; 
    put _all_; 
run; 

表明,「標籤」變量名並不像它看起來:

22   data _NULL_; 
23   set stats; 
24   put _all_; 
25   run; 

Variable=x Label =Variable X Mean=0.461116 _ERROR_=0 _N_=1 
Variable=y Label =Variable Y Mean=0.525342 _ERROR_=0 _N_=2 

通知變量名「標籤」之間的空間和等號。沒有其他變量是這樣的。也許變量的名稱已損壞。

負載變量名從dictionary.columns表到另一個表,並期待在值:

proc sql; 
    create table x as 
    select name as nm from dictionary.columns 
    where libname = 'WORK' and memname = 'STATS'; 
quit; 

data _NULL_; 
    set x; 
    put nm= nm $hex32.; 
run; 

$HEX32.格式轉換的文本轉換成ASCII碼,所以你可以看到,如果有任何非打印字符在那裏。此datastep的輸出是:

22   data _NULL_; 
23   set x; 
24   put nm= nm $hex32.; 
25   run; 

nm=Variable 5661726961626C652020202020202020 
nm=Label 4C6162656C0000002020202020202020 
nm=Mean 4D65616E202020202020202020202020 

在標籤變量尋找,首先將間隙仍顯示它與下一個輸出之間。十六進制代碼包含了一些重複零:

4C6162656C0000002020202020202020

4C=L 
61=a 
62=b 
65=e 
6C=l 
00=? 
20=<space> 

所以這是在「標籤」變量名,這些ASCII零所導致的問題。 SAS只能將其顯示爲'標籤',其中這些ASCII零(a.k.a ASCII空白)顯示爲空格。

修復

我不知道的方法指包含ASCII特殊字符列,所以我們可以做的是重新命名列。但是,我們仍然無法通過名稱來引用「標籤」,因此我們需要間接參考它。一種方法是使用陣列:

data stats_fix; 
    set stats; 
    array c{*} _CHARACTER_; 
    var=c[1]; 
    Label=c[2]; 
run; 

查看輸出數據集很奇怪。數據集有兩個變量,稱爲'標籤'。我們知道一個是'Label',另一個是'Label000000'。

作爲PROC MEANS中的一個錯誤,可能需要將SAS技術支持提出來,請隨意使用盡可能多的答案。