DATA Step, Macro, Functions and more

Deleting records with last.variable with a multi conditional if statement

Reply
Contributor
Posts: 40

Deleting records with last.variable with a multi conditional if statement

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;
Super User
Posts: 11,343

Re: Deleting records with last.variable with a multi conditional if statement

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

Super User
Super User
Posts: 7,955

Re: Deleting records with last.variable with a multi conditional if statement

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

Trusted Advisor
Posts: 1,117

Re: Deleting records with last.variable with a multi conditional if statement

[ Edited ]

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;
Respected Advisor
Posts: 4,923

Re: Deleting records with last.variable with a multi conditional if statement

Posted in reply to FreelanceReinhard

@FreelanceReinhard, interesting, but harmful knowledge. I hope I will never have to look at such coding!

PG
Ask a Question
Discussion stats
  • 4 replies
  • 253 views
  • 2 likes
  • 5 in conversation