2014-09-29 282 views
0

我需要創建一個頁腳記錄,顯示與表中列數相等的分號。該表有47列,但這可能會改變,所以我需要動態計算列而不使用字典表。頁腳記錄將被導出到一個CSV文件,它的分隔符是分號。動態計算SAS表中的列數

我試過下面的代碼,它可以工作,但是當把數據導出到csv文件時,即使我使用了dequote函數,分號也被雙引號括起來。任何想法刪除它們?提前致謝。

proc contents data=input out=output noprint; run; 
proc sort data=output; by varnum; run; 
    data _null_; 
    set output; 
    call symputx("maximum",varnum); 
    run; 
    data semis; 
    length ffsemis $47; 
    ffsemis = repeat(';',&maximum*1); 
* call symputx("semis",ffsemis); 
    run; 
    data _null_; 
    file "E:\semis.csv" delimiter=';' dsd dropover; 
    set work.semis; 
    if _n_=1 then do; 
    put ffsemis; 
    end; 
    run; 
+0

目前,您的邏輯似乎基於數據集中變量的數量,而不是行數。你確定你想要分號=行嗎? – user667489 2014-09-29 07:24:28

+0

對不起,我的意思是計算數據集中的列或變量。是的,這是變更請求的要求。 – 2014-09-29 08:01:30

+0

更改請求的要求是分號等於列數。 47列= ;;;;;;;;;;;;;;;;;;;;;;;###################################################################### – 2014-09-29 09:49:36

回答

0

我能想到的確定SAS數據集中變量數量的最簡單方法是使用proc內容。

創建一個包含數據內容的新數據集,並運行一個空數據步驟以將最大變量數保存爲宏變量。請注意,宏變量被保存爲字符值,因此您必須通過對該值執行數字運算來將其轉換回數字,以便在代碼中將新宏變量用作數字。

proc contents data=input out=output; run; 

proc sort data=output; by varnum; run; 

data _null_; 
    set output; 
    call symputx("maximum",varnum); 
run; 

data semis; 
    length ffsemis $27; 
    ffsemis = repeat(%str(;),&maximum*1); 
    call symputx("semis",ffsemis); 
run; 

%put SEMIS = "&semis."; 

proc export data=semis file="C:\semis.csv"; 
run; 
+0

嗨,謝謝你的輸入。我試過你的代碼,它能夠獲得最大數量的列。但是當我使用REPEAT函數獲得相同數量的分號時,由於REPEAT函數和分號是字符,所以它失敗了。有關這種情況的任何建議?提前致謝。 – 2014-09-30 05:55:00

+0

我添加了下面的代碼,它似乎正常工作。一個變量用許多分號創建。數據半導體; ; ffsemis = repeat(';',&maximum * 1); 跑; – JJFord3 2014-09-30 12:35:25

+0

感謝JJFord3,我試過你的代碼將數據導出到csv文件,分號仍然用雙引號括起來。 – 2014-10-01 02:39:51