作者:凌亂的回譩 | 来源:互联网 | 2023-05-17 20:47
Iamtryingtowriteamacrothatwillcreateanewtabledependentoncertaincriteria.Theuserca
I am trying to write a macro that will create a new table dependent on certain criteria. The user can enter a single number or a range of numbers to filter the original data on.
我正在尝试编写一个宏,它将根据某些条件创建一个新表。用户可以输入单个数字或一系列数字来过滤原始数据。
For example, if the starting data set is shown below, the end user can choose to filter Col Z by a single number (ie: where Z=1) or a range of numbers (ie: where Z is between 1 and 5).
例如,如果起始数据集如下所示,则最终用户可以选择通过单个数字(即:其中Z = 1)或数字范围(即:其中Z在1和5之间)过滤Col Z.
X Y Z
0 1 3
2 7 9
0 0 0
2 1 1
If the user wants to enter a range of numbers to filter on, there will be values populated for &start_range and &end_range. If the user wants to filter on a single number, &end_range will equal null.
如果用户想要输入要过滤的数字范围,则会为&start_range和&end_range填充值。如果用户想要对单个数字进行过滤,则&end_range将等于null。
I want code that will check if &end_range equals null. If it does, the where statement should create a new table for all rows where column Z=&start_range. If &end_range is NOT equal to null (it has a value populated), the where statement will create a new table for all rows where column Z is between &start_range and &end_range.
我想要检查&end_range是否等于null的代码。如果是,则where语句应为列Z =&start_range的所有行创建一个新表。如果&end_range不等于null(它有一个填充的值),where语句将为列Z在&start_range和&end_range之间的所有行创建一个新表。
For a single value, (where &start_range=1 and &end_range=’ ‘), the resulting table would be:
对于单个值,(其中&start_range = 1和&end_range =''),结果表将是:
X Y Z
2 1 1
For a range of values, (where &start_range=1 and &end_range=5), the resulting table would be:
对于一系列值,(其中&start_range = 1和&end_range = 5),结果表将是:
X Y Z
0 1 3
2 1 1
This is the code I have so far, not 100% sure if syntax is correct so open to any and all suggestions, thanks!!:
这是我到目前为止的代码,不是100%确定语法是否正确所以对任何和所有建议开放,谢谢!!:
%macro filter();
proc sql;
create table filtered_data AS
select raw_data.*
from raw_data
WHERE
%if &end_range NE '' %then %do;
Z between '&start_range' AND '&end_range';
%end;
%else %do;
Z = '&start_range';
%end;
quit;
%mend;
%filter();
1 个解决方案