2015-02-24 140 views
2

取樣輸入數據:
FirstName LastName Group Age LastVenue Position
Jack Smith ULDA 25 TheaterA 1
Jesse James GODL 37 TheaterB 12
Jane Doe ULDA 29 TheaterA 3
Izzy Gord IIPA 41 TheaterC 8
Ann Roswell GODL 30 TheaterB 16
Chelsea Jenk ULDA 19 TheaterA 11
如何用條件創建這個宏?

我想創建:
%macro group_members(group=); proc print data=sample; var Position Age Group FirstName LastName; where group=&group; %mend group_members;

但是我想爲它添加條件,所以如果沒有輸入任何內容%group_members()那麼它將顯示所有組的變量順序如上所示。如果在這種情況下輸入了一個無效的組:%group_members(LOL)那麼我希望將一個註釋發送到日誌%put 'An invalid group was entered'。因此沒有什麼應該打印。我正嘗試在一個更大的數據集上創建一個非常相似的程序。
我感謝任何幫助先進!謝謝:)

到目前爲止,我曾嘗試:
%macro group_members(group=); proc sql; select count(*) into :ct from sample where group="&group" quit; proc print data=sample; %if &group ^= %then %do; where group="&group."; %end; %if &ct = 0 %then %put An Invalid group was entered; %else %do; where group="&group."; %end; run; %mend group_members;

我得到的錯誤,從每一個測試。例如%group_members()返回一個錯誤:
ERROR: More positional parameters found than defined

回答

1
  1. 輸入空導致顯示的所有組可以通過用以下宏代碼圍繞where聲明來實現:
 
    %if &group ^= %then %do; 
     where group="&group."; 
    %end; 

這隻會提交where聲明,如果填充了&group變量。還要注意,我已經添加了雙引號,因此where語句不會生成語法錯誤。

  1. 該宏需要知道哪些組是有效的或無效的。這就要求proc print前一個額外的處理步驟:
 
    proc sql; 
     select count(*) into :ct 
     from sample 
     where group="&group"; 
    quit; 

    %if &ct = 0 %then %put An invalid group was entered; 
    %else %do; 
    ... 

& CT包含的where條款相匹配的記錄數。如果爲零,那麼我假設這意味着它是一個無效的組。

+0

謝謝你的代碼中的註釋!我是SAS Macro的新手。我會放置proc sql; proc打印之前的宏內部? – Jax 2015-02-24 01:17:22

+0

是的,'&group'宏變量在宏外不可用,所以它在外面是沒有意義的。 – mjsqu 2015-02-24 01:18:26

+0

我試過這個,但我不能得到它的工作......這是我有:'%macro group_members(group =); \t proc sql; \t select count(*)into:ct \t from sample where group =「&group」 quit; \t proc print data = sample; \t%if&group^=%then%do; \t where team =「&group。」; \t%end; \t%if&ct = 0%then%put一個無效組被輸入; \t%else%do; \t其中group =「&group。」; \t%end; \t run; %修補group_members; %group_members()' – Jax 2015-02-24 02:26:05

0

感謝@mjsqu

第一步:測試是否&group是有效的組。 count(*)爲你做這個。

第2步:如果count(*)返回0,則輸出用戶定義的消息。

第3步:否則,繼續proc print。如果&group =則列出所有記錄。

%macro group_members(group); 
    proc sql noprint; 
    select count(*) into :ct 
    from sample 
    where group="&group."; 
    %if &ct = 0 and &group ne %then %put An Invalid group was entered; 
    %else %do; 
    proc print data=sample; 
    var Position Age Group FirstName LastName; 
    %if &group ne %then 
    %do; 
    where group="&group."; 
    %end; 
    %end; 
%mend group_members; 
%group_members(); 
%group_members(GODL); 
%group_members(G); 
+0

我會建議在'proc sql'後面添加'noprint'。它會在'proc print'結果之上隱藏'&ct'的值。 – Lovnlust 2015-02-24 03:10:55

+0

當Group爲空時,您的代碼仍然會打印 - 輸入了一個無效的組,也不會輸出任何內容,我不認爲OP在尋找什麼。 – NEOmen 2015-02-24 05:05:22

0

/創建示例數據集/

data test; 
infile datalines dlm="," missover; 
input FirstName : $10. 
     LastName : $10. 
     Group : $8. 
     Age : 8. 
     LastVenue : $10. 
     Position : 8. 
     ; 
     datalines; 
Jack,Smith,ULDA,25,TheaterA,1 
Jesse,James,GODL,37,TheaterB,12 
Jane,Doe,ULDA,29,TheaterA,3 
Izzy,Gord,IIPA,41,TheaterC,8 
Ann,Roswell,GODL,30,TheaterB,16 
Chelsea,Jenk,ULDA,19,TheaterA,11 
; 
run; 

是否加入本身

%macro group_members(group=); 
%put &group.; 

/*Checking if the group is valid or invalid*/ 
proc sql noprint; 
select count(*) into :num from test where group="&group."; 
quit; 
%put &num.; 


data final; 
set test; 

/*checking if the group entered is NULL, if it is ,then it will output all the records*/ 
%if "&group."="" %then %do; %end; 


/*If the group is Valid or not, if it is invalid then nothing will be in output and a msg in the LOG will be displayed, you can put ERROR statement if you want*/ 
%else %if &num. = 0 %then %do; 
where group="&group."; 
%put "An Invalid group was entered"; 

/*If above two are not the case then it will filter on that group*/ 
%end; 
%else %do; 
where group="&group."; 
%end; 
run; 

%mend group_members; 

%group_members(group=);