The two mathods get same result but what is their difference?
Thanks!
data class2;
set sashelp.class;
if age=12;
run;
data class3;
set sashelp.class;
where age=12;
run;
There is no difference at this instance but there is a difference at other instances. One example would be
data exam;
input name $ class $ score ;
if name = 'Tim' or name = 'Sally';
cards;
Tim math 9
Tim history 8
Tim science 7
Sally math 10
Sally science 7
Sally history 10
John math 8
John history 8
John science 9
;
run;
proc print; run;
Output:
Obs name class score
1 Tim math 9
2 Tim history 8
3 Tim science 7
4 Sally math 10
5 Sally science 7
6 Sally history 10
data exam;
input name $ class $ score ;
where name = 'Tim' or name = 'Sally';
cards;
Tim math 9
Tim history 8
Tim science 7
Sally math 10
Sally science 7
Sally history 10
John math 8
John history 8
John science 9
;
run;
proc print; run;
Log:
736 data exam;
737 input name $ class $ score ;
738 where name = 'Tim' or name = 'Sally';
ERROR: No input data sets available for WHERE statement.
739 cards;
There is no difference at this instance but there is a difference at other instances. One example would be
data exam;
input name $ class $ score ;
if name = 'Tim' or name = 'Sally';
cards;
Tim math 9
Tim history 8
Tim science 7
Sally math 10
Sally science 7
Sally history 10
John math 8
John history 8
John science 9
;
run;
proc print; run;
Output:
Obs name class score
1 Tim math 9
2 Tim history 8
3 Tim science 7
4 Sally math 10
5 Sally science 7
6 Sally history 10
data exam;
input name $ class $ score ;
where name = 'Tim' or name = 'Sally';
cards;
Tim math 9
Tim history 8
Tim science 7
Sally math 10
Sally science 7
Sally history 10
John math 8
John history 8
John science 9
;
run;
proc print; run;
Log:
736 data exam;
737 input name $ class $ score ;
738 where name = 'Tim' or name = 'Sally';
ERROR: No input data sets available for WHERE statement.
739 cards;
Thanks!
Also, take a look at: http://www2.sas.com/proceedings/sugi31/238-31.pdf
some other differences:
data class3;
set sashelp.class(where=(age=12));/*you can't replace where with if */
run;
/***********************************/
data class4;
set sashelp.class;
newage=age+2;
if newage>15;/*you can't replace if with where */
run;
/*********************************/
proc print data=sashelp.class;
where age>14;
run;/*you can't replace where with if */
/*************************************/
data class5;
set sashelp.class;
where age between 12 and 14;
run;/*you can't replace where with if */
Linlin
Please correct me if I am wrong, my not-so-accurate memory tell me that the major difference between where and if lies on the where they function.
'if' acts in between pdv and output, also in a sequential way; while 'where' plays before pdv in a random access way, that is also why 'where' can work with index.
Haikuo
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 the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.