BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

In order to check if a data set has index I saw the following code:

%let dsid=%sysfunc(open(sashelp.cars));
%put dsid=&dsid; /**2**/

%let indx=%sysfunc(attrn(&dsid,isindex));
%put indx=&indx; /**0**/

My question-

&dsid  get value 2 

what does it mean?

What exactly &dsid is calculating?

 

In code : %let indx=%sysfunc(attrn(&dsid,isindex));

why the argument is &dsid  and not data set name (sashelp.cars)?

 

http://support.sas.com/kb/43/637.html

 

3 REPLIES 3
Patrick
Opal | Level 21

From the docu for the 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.

From experience:

The first time you use the open() function in a SAS session, the function will return a value of 1 if successful. Any time you execute the function again in the same session the return value gets incremented by 1. 

 

Think of this value as a pointer to your opened data set that you then use in other functions.

 

Tom
Super User Tom
Super User

The value returned by OPEN() is the something called a "file handle" in a lot of programming languages.  It is essentially a pointer to the open file that you can pass to other functions that operate on files, like the ATTRN() function (and the CLOSE() function) used in that example.

 

You cannot just use the filename (or in this case the dataset name) because you might have the same dataset open multiple times for different purposes.

 

Consider this simple SAS program to do a quick sort of SASHELP.CLASS by SEX.

data class ;
  set sashelp.class(where=(sex='F'))
      sashelp.class(where=(sex='M'))
  ;
run;
yabwon
Amethyst | Level 16

I highly recommend Maxim number 1, it's not that hard to do.

 

Open() Function - Details:

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0q72zlfmfjplon1p3nprf85sdin.h...

 

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



hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 641 views
  • 0 likes
  • 4 in conversation