Could you let me know how to write the logic for handling below condition in the code:
How to handle column1>=7 logic
=====================================
%macro test(column_value=,out_dataset=);
data &out.;
set Table1;
if column1=&column_value;
run;
%mend test();
%test(column_value=3,out_dataset=group1);
%test(column_value=5,out_dataset=group2);
if column1>=7 then create group3 dataset /How to handle >= logic
Below should neatly do it:
%macro test(column_value=,out_dataset=);
data &out_dataset.;
%*Input dataset to read;
set Table1;
%* Condition required to subset the dataset;
%if %sysevalf(&column_value. < 7) %then
%do;
if column1 = &column_value. then
output;
%end;
%if %sysevalf(&column_value.) >= 7 %then
%do;
if column1 >= &column_value. then
output;
%end;
run;
%mend;
Is column1 the name of a column or are you trying to refer to the first column where the name may be dynamic?
FYI I think your data statement should be:
data &out_dataset..;
You cannot have different logic within the same macro so you need to decide to either:
Your request means changing the code logic so your process has to be more dynamic in some fashion.
@Lav001 wrote:
Could you let me know how to write the logic for handling below condition in the code:
How to handle column1>=7 logic
=====================================
%macro test(column_value=,out_dataset=);
data &out.;
set Table1;
if column1=&column_value;
run;
%mend test();
%test(column_value=3,out_dataset=group1);
%test(column_value=5,out_dataset=group2);
if column1>=7 then create group3 dataset /How to handle >= logic
Column1 refers to one particular column in the table.
I need to create groups based on below Column1 values:
=2
=3
=4
>=4
=5
>=5
is there a way to pass the above Column1 value while calling the Macro instead of using separate conditions for each value.
@Lav001 wrote:
Column1 refers to one particular column in the table.
I need to create groups based on below Column1 values:
=2
=3
=4
>=4
=5
>=5
is there a way to pass the above Column1 value while calling the Macro instead of using separate conditions for each value.
You have answered your own question.
%macro test(condition=,out_dataset=);
data &out_dataset;
set Table1;
if column1 &condition ;
run;
%mend test;
%test(condition= =2 ,out_dataset= set1)
%test(condition= =3 ,out_dataset= set2)
%test(condition= =4 ,out_dataset= set3)
%test(condition= >=4 ,out_dataset= set4)
%test(condition= =5 ,out_dataset= set5)
%test(condition= >=5 ,out_dataset= set6)
Hi @Lav001,
You could use something like this:
%macro test(cond,column_value=,out_dataset=);
data &out_dataset;
set Table1;
%if %length(&cond) %then if column1 %substr(&cond,%sysfunc(findc(&cond,<>)));
%else if column1=&column_value;;
run;
%mend test;
Then you can call the macro as intended (and also conditions with ">", "<" and "<=" are possible):
%test(column_value=3,out_dataset=group1)
%test(column_value=5,out_dataset=group2)
%test(column_value>=7,out_dataset=group3)
%test(column_value<=2,out_dataset=group4)
%test(column_value>8,out_dataset=group5)
%test(column_value<6,out_dataset=group6)
Edit: Conditions with, say, "~=" can be implemented as well: Just insert the "~" in the second argument of the FINDC function: <~>.
Include a macro argument OPERATOR, which can take on values eq ge gt le lt ne (or it can take on the values using the greater than sign, less than sign and equal sign)
Example:
%macro test(column_value=,out_dataset=,operator=);
data &out_dataset.;
set sashelp.cars;
if cylinders &operator &column_value;
run;
%mend test;
%test(column_value=4,operator=eq,out_dataset=group1)
%test(column_value=6,operator=eq,out_dataset=group2)
%test(column_value=6,operator=ge,out_dataset=group3)
%test(column_value=6,operator=>=,out_dataset=group4)
Which brings up the question: why do you need separate data sets to do this? What is the benefit of separate data sets (and macros) compared to keeping one large data set and doing whatever analysis comes next on the one large data set? If you use custom formats, you can get analyses of all the conditions you specified above, without macros and without creating separate data sets.
I need to create groups based on below Column1 values:
=2
=3
=4
>=4
=5
>=5
Example:
proc format;
value custf (multilabel) 2='2' 3='3' 4='4' 4-high='>=4' 5='5' 5-high='>=5';
run;
proc means data=sashelp.cars;
class cylinders/mlf;
var mpg_city;
format cylinders custf.;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.