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

4

3 回答 3

1
  1. Entering a blank resulting in all groups being shown could be achieved by surrounding the where statement with this macro code:
    %if &group ^= %then %do;
      where group="&group.";
    %end;

This only submits the where statement, in the event that the &group variable is populated. Note also that I've added double quotes so that the where statement doesn't generate syntax errors.

  1. The macro would need to know which groups were or were not valid. This would require an extra processing step before the 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 will contain the number of records that match the where clause. If zero, then I'm assuming that means it's an invalid group.

于 2015-02-24T01:10:02.093 回答
0

感谢@mjsqu

Step1:测试是否&group是一个有效的组。count(*)为你做这个。

Step2:如果count(*)返回0,则输出自定义消息。

Step3:否则,继续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);
于 2015-02-24T03:09:39.220 回答
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=); 
于 2015-02-24T03:14:00.623 回答