BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Barite | Level 11

Hi Everyone,

In my original code below, SAS processes all rows.

However, I only want SAS to process rows with day_of_interest=1.

I put %IF into the code but it doesn't work.

(In my actual work, it is a marco process)

Can anyone please help me to fix it?

Thank you,

HHC

data have; 
input id sale max min day_of_interest;
datalines;
1 5 70 4 0
2 2 70 4 0
3 10 30 4 1
4 100 50 17 1
5 15 50 14 0
;run;

data want;
set have nobs=totalobs;
drop i new_id new_max new_min;
exit_id=.;
i+1;
	do j=i+1 to totalobs until (exit_id^=.);
		set have (keep = id max min rename=(id=new_id max=new_max min=new_min)) point=j;
		if sale>new_max or sale<new_min then exit_id=new_id;
	end;
run;

*------------MACRO----------------------;
%Macro xx;
data want;
set have nobs=totalobs;
exit_id=.;
i+1;
%IF day_of_interest=1 %THEN %DO;
	do j=i+1 to totalobs until (exit_id^=.);
		set have (keep = id max min rename=(id=new_id max=new_max min=new_min)) point=j;
		if sale>new_max or sale<new_min then exit_id=new_id;
	end;
%END;
run;
%MEnd;
%xx;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@hhchenfx wrote:

My real work is a macro, so I make this to replicate the situation.

 


That doesn't mean you have to use the macro %IF in this specific situation, especially since it will not work here. A macro %IF cannot evaluate the value of a data step variable to see if its value is equal to 1, as you are using here. A data step IF can evaluate the value of the data step variable to see if it is equal to 1.

--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Try using DATA step IF THEN ELSE instead of macro %IF %THEN %ELSE

--
Paige Miller
hhchenfx
Barite | Level 11

My real work is a macro, so I make this to replicate the situation.

Thanks,

HHC

PaigeMiller
Diamond | Level 26

@hhchenfx wrote:

My real work is a macro, so I make this to replicate the situation.

 


That doesn't mean you have to use the macro %IF in this specific situation, especially since it will not work here. A macro %IF cannot evaluate the value of a data step variable to see if its value is equal to 1, as you are using here. A data step IF can evaluate the value of the data step variable to see if it is equal to 1.

--
Paige Miller
ChrisNZ
Tourmaline | Level 20

1. The macro language should only be used when necessary, as it can confuse untrained users.

2. A trained user would know that the macro processor cannot know the value of a data step variable.

Hence @PaigeMiller 's valid recommendatoin.

hhchenfx
Barite | Level 11

Thank you all for helping.

I got it

data want;
set have nobs=totalobs;
drop i new_id new_max new_min;
exit_id=.;
i+1;
if day_of_interest=1 then do;
	do j=i+1 to totalobs until (exit_id^=.);
		set have (keep = id max min rename=(id=new_id max=new_max min=new_min)) point=j;
		if sale>new_max or sale<new_min then exit_id=new_id;
	end;
end;
run;

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 25. 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
  • 5 replies
  • 640 views
  • 0 likes
  • 3 in conversation