- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Try using DATA step IF THEN ELSE instead of macro %IF %THEN %ELSE
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
My real work is a macro, so I make this to replicate the situation.
Thanks,
HHC
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;