BookmarkSubscribeRSS Feed
capam
Pyrite | Level 9

Hello,

 

I have the following code that generates an error. I am checking the matrix 'have' for the variable 'cusum'. If 'cusum' exists it is removed. The error I get is: 

 

ERROR 117-185: There was 1 unclosed DO block.

 

	data _null_;
		dsid=open('have');
		check=varnum(dsid,'cusum');

		if check=1 then
			do;
	data have(drop=cusum);
	set have;
	run;
			end;
		run;
3 REPLIES 3
PaigeMiller
Diamond | Level 26

You can't have a data step inside a data step. That is the cause of the ERROR message you are seeing.

 

A better way to check to see if a variable exists and then delete it would be:

 

Use PROC SQL to query the DICTIONARY.COLUMNS table to see if the variable exists; and if it exists, then use PROC DATASETS to remove the variable from the data set. This would have to be done in a macro.

 

I'm curious as to why you need to conditionally delete a variable if it exists, as normally leaving an unwanted variable in a data set causes no problems other than taking up slightly more disk space.

--
Paige Miller
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Something like the below - although I totally agree with @PaigeMiller, there is rarely a need to delete a variable if you don't know about it up front, sounds like a lack of knowledge about your data.

data _null_;
  set sashelp.vcolumn (where=(libname="WORK" and memname="HAVE" and name="CUSUM"));
  call execute('data work.have; set work.have (drop=cusum); run;');
run;
Quentin
Super User

You don't actually need to check for the existence of the variable. 

 

The system option DKRICOND determines whether or not and error is generated when you drop/keep/rename a variable that doesn't exist.  It's default is DKRICOND=ERROR, but you can set it to NOWARN.

 

data have1;
  x=1;
run;
data have2;
  x=2;
  cusum=1;
run;

options dkricond=error;
data want;
  set have1(drop=cusum)  /*causes error cuz cusum does not exist*/
      have2(drop=cusum)
  ;
  put _all_;
run;


options dkricond=nowarn;
data want;
  set have1(drop=cusum)  /*no error cuz DKRICOND=NOWARN*/
      have2(drop=cusum)
  ;
  put _all_;
run;
options dkricond=error;  *restore the default;
BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 3 replies
  • 713 views
  • 0 likes
  • 4 in conversation