BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
slacey
Obsidian | Level 7

In the output method for the hash package, how do I specify overwrite? (is it even possible?). For example, the following code runs fine the first time:

 

proc ds2;
	data _null_;
		dcl char(20) USUBJID;
		dcl char(8) VSTESTCD;
		dcl char(20) VISIT;
		dcl char(30) VSTPT;
		dcl double rc;

		dcl package hash hw();

		method init();
			hw.ordered('a');
			hw.keys([USUBJID VSTESTCD VISIT VSTPT]);
			hw.dataset(%tslit({select * 
					   from sdtm.vs 
					   where VSTESTCD in ('HEIGHT','WEIGHT')}));
			hw.definedone();
		end;

		method term();
			rc = hw.output('testtest5');
		end;
	enddata;
	run;
quit;

However if I run again the dataset that hw.output tries to use produces an error since it already exists.

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20
I don't think such an option exists but might be wrong.

You can work around that by calling function dosubl() to run proc delete before calling the output method.

View solution in original post

7 REPLIES 7
ChrisNZ
Tourmaline | Level 20
I don't think such an option exists but might be wrong.

You can work around that by calling function dosubl() to run proc delete before calling the output method.
slacey
Obsidian | Level 7

Looks like dosubl isn't recognized in proc ds2 (or it's very possible i was using it wrong). However I was able to just pass it through a sysfunc macro call like so:

 

		%if %sysfunc(exist(testtest1)) %then %do;
			%sysfunc(dosubl('proc delete lib=work data=testtest1; run;'));
		%end;

Thank you for the suggestion!

Tom
Super User Tom
Super User

@slacey wrote:

Looks like dosubl isn't recognized in proc ds2 (or it's very possible i was using it wrong). However I was able to just pass it through a sysfunc macro call like so:

 

		%if %sysfunc(exist(testtest1)) %then %do;
			%sysfunc(dosubl('proc delete lib=work data=testtest1; run;'));
		%end;

Thank you for the suggestion!


Why would you need DOSUBL in that case?  How is running that macro code different than running this macro code?

%if %sysfunc(exist(testtest1)) %then %do;
  proc delete lib=work data=testtest1; run;
%end;

Or this SAS code (note that PROC DELETE does not have a NOWARN option, but PROC DATASETS does).

proc datasets nowarn nolist lib=work;
  delete testtest1;
quit;
slacey
Obsidian | Level 7

You're 100% correct. I wrote the reply in the middle of working on it and came across that exact realization afterwards. In the end I just wrapped the proc delete in the macro exist call and put it all before proc ds2.

AhmedAl_Attar
Ammonite | Level 13

@slacey 

You probably can add the following before your Proc DS2 statement

%if (%sysfunc(exist(testtest5))) %then
%do;
   PROC DELETE Data=testtest5; run;
%end;
yabwon
Onyx | Level 15

Use the Force 😉

 

When a Jedi Knight uses PROC FEDSQL, the proc just deletes data set regardless the data set exists or not 🙂

data class; 
  set sashelp.class;
run;


PROC FEDSQL;
   DROP TABLE TESTTEST5 FORCE; /* no need for conditional execution */
QUIT;

proc ds2;
	data _null_ / OVERWRITE=YES;
		dcl char(20) name;
		dcl char(8) sex;
		dcl double AGE;
		dcl double rc;

		dcl package hash hw();

		method init();
			hw.ordered('a');
			hw.keys([name age]);
			hw.dataset(%tslit({select * 
					   from work.class 
					   where sex in ('F','M')}));
			hw.definedone();
		end;

		method term();
			rc = hw.output('testtest5');
		end;
	enddata;
	run;
quit;

May the Force be with you!

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



slacey
Obsidian | Level 7
I really like that. I've been using SAS for years now and it's enlightening to see how much I don't know.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

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
  • 7 replies
  • 543 views
  • 6 likes
  • 5 in conversation