BookmarkSubscribeRSS Feed
stataq
Quartz | Level 8

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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1186 views
  • 1 like
  • 3 in conversation