BookmarkSubscribeRSS Feed
stataq
Obsidian | Level 7

Hello,

 

I have a question on %sysfunc(open())

 

I thought  %sysfunc(open()) is open a dataset, which give 0 or 1 to show whether the dataset is opened successfully.  I accidentally forgot to close the file, and found that if I keep running following code, &_dsid is not a 0,1  value, instead it will keep going up to 2, 3, 4.

 

Can anyone explain what  _dsid really is? 

and also what varnum(1, &_colvar) is. why 1 instead of not a dataset name here?

%let _dsid=%sysfunc(open(sashelp.baseball));
%put &_dsid;
%let _colvar=team;
%let _varnum=%sysfunc(varnum(&_dsid,&_colvar));
%put &_varnum;

Thanks.

3 REPLIES 3
Kurt_Bremser
Super User

Maxim 1: Read the Documentation.

From OPEN Function

The OPEN function opens a SAS data set, DATA step, or a SAS SQL view and returns a unique numeric data set identifier, which is used in most other data set access functions. OPEN returns 0 if the data set could not be opened.

Tom
Super User Tom
Super User

If you want to know what %SYSFUNC(OPEN()) returns then ignore the use of the %SYSFUNC() wrapper macro function and just look at the documentation of the actual OPEN() function.

 

It does NOT return a boolean 0/1 answer.  Instead it returns the File handle (or in this case what the documentation calls a table-id) that is required to be used by the other functions that can operate on an open dataset.

 

If the OPEN() fails then it returns 0 instead of an actual number.

 

If the OPEN() succeeds then you need to make sure to call CLOSE(), passing the in the file handle OPEN() generated, so you close it.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 3 replies
  • 269 views
  • 1 like
  • 3 in conversation