BookmarkSubscribeRSS Feed
Lav001
Fluorite | Level 6

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

7 REPLIES 7
qoit
Pyrite | Level 9

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;
Reeza
Super User

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:

  • handle this separately, add in an exception for this specific case
  • or pass the entire logic to the macro each time

 

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


 

Lav001
Fluorite | Level 6

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.

 

Tom
Super User Tom
Super User

@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)

 

FreelanceReinh
Jade | Level 19

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: <~>.

PaigeMiller
Diamond | Level 26

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;

 

 

--
Paige Miller
Reeza
Super User
I'll echo @PaigeMiller is saying a multilabel format is likely a better approach here.

SAS Innovate 2025: Call for Content

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 821 views
  • 4 likes
  • 6 in conversation