I want to delete the last record if certain conditions apply. However, I am getting the following error:
ERROR 68-185: The function CONTAINS is unknown, or cannot be accessed.
Here is what I am working on:
data test;
format date date9.;
input sn $
date
ctry $
source $;
datalines;
1 20000 US PL
1 20001 US PL
1 20005 US CIM /*this would be removed*/
2 20000 CA SOS
2 25000 FR CIM /*this would be kept*/
;
run;
proc sort data = test;
by sn date ctry;
run;
data test2;
set test;
by sn;
if last.ctry = lag1(last.ctry) and (abs(last.date - lag1(last.date) < 180)
and source contains ('CIM') then delete;
run;
Simple SAS does not have a datastep condition CONTAINS though it is availabe in Proc SQL. There are a number of other methods. I would likely use
... and index(source,'CIM') > 0 then
If source might actual have lower case versions such as "cim" "Cim" "CiM" that you want to match as well then use
index( Upcase(source),'CIM') >0
You will have read the documentation yes?
http://support.sas.com/kb/43/303.html
Contains only works on where clauses, you want:
and index(source,"CIM") > 0 then...
Also, what do you expect LAST.CTRY and LAST.DATE to contain, given that neither CTRY nor DATE appears in the BY statement (cf. section "Processing BY Groups" in the documentation)?
-------------------
Side note: It's interesting to see that FIRST.xxx and LAST.xxx variables with arbitrary names xxx can be freely used as temporary (i.e. automatically dropped) variables which are automatically retained and initialized to zero (not as index variables of DO loops, though). Fun example:
data test;
set sashelp.class end=eof;
first.xy=sqrt(first.xy+1);
last.abc=1/(last.abc+1);
if eof then put first.xy= / last.abc= ;
run;
@FreelanceReinh, interesting, but harmful knowledge. I hope I will never have to look at such coding!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.