DATA Step, Macro, Functions and more

%SYSFUNC open

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

%SYSFUNC open

Hello all, 

 

I used two lines to open a dataset in my macro. 

 

%let dataset=temp;

%let nobs=%sysfunc(attrn(%sysfunc(open(&ds)),NOBS));

 

I used the sysfunc close in order to close, but I learned that my dataset was not closed because I couldn't make changes to the dataset.

 

Can anyone tell me what I can do to close the dataset?

 

Thanks. 

 

Bo klauth


Accepted Solutions
Solution
‎06-08-2018 04:44 PM
PROC Star
Posts: 632

Re: %SYSFUNC open

SASHELP.VTABLE or DICTIONARY.TABLES are read only tables that contains SAS session information. It contains lot of useful information about tables like column names, format, number of recods...etc. 'nobs' is one peice in it that gives the count on a dataset. 

 

PROC SQL; 
create table test as
select * 
from sashelp.vtable
where libname="SASHELP" and memname="CLASS"
;
quit;

Run this and see what all information it can give. LIBNAME=the library where your table is, memname= you dataset name

Thanks,
Suryakiran

View solution in original post


All Replies
PROC Star
Posts: 632

Re: %SYSFUNC open

If your trying to get the number of observation in a SAS Dataset then why not query the VTABLE

 


PROC SQL; 
select nobs INTO: nobs 
from sashelp.vtable
where libname="SASHELP" and memname="CLASS"
;
quit;

%PUT "Number of Observetions:&nobs";
Thanks,
Suryakiran
Occasional Contributor
Posts: 8

Re: %SYSFUNC open

Posted in reply to SuryaKiran

Hello Suryakiran, 

 

Thanks for your response. I used the whole thing you wrote. The result was 19. Would I need to chang anything? I have 45 observations in my dataset. 

 

Thanks. 

 

Bo Klauth

Solution
‎06-08-2018 04:44 PM
PROC Star
Posts: 632

Re: %SYSFUNC open

SASHELP.VTABLE or DICTIONARY.TABLES are read only tables that contains SAS session information. It contains lot of useful information about tables like column names, format, number of recods...etc. 'nobs' is one peice in it that gives the count on a dataset. 

 

PROC SQL; 
create table test as
select * 
from sashelp.vtable
where libname="SASHELP" and memname="CLASS"
;
quit;

Run this and see what all information it can give. LIBNAME=the library where your table is, memname= you dataset name

Thanks,
Suryakiran
Occasional Contributor
Posts: 8

Re: %SYSFUNC open

Posted in reply to SuryaKiran

hi, 

Could you please tell me more how I can get the number of observations to use? I ran your code, it appeared to be working, but I didn't see the number of observations. 

 

Thanks. 

Contributor
Posts: 38

Re: %SYSFUNC open

The nobs column contains the number of observations, its label is "Number of Physical Observations".

 

Here is a document with some information about the SASHELP Views: http://www2.sas.com/proceedings/sugi26/p017-26.pdf

Occasional Contributor
Posts: 8

Re: %SYSFUNC open

Thanks. 

Super User
Posts: 10,616

Re: %SYSFUNC open

Insert your library and dataset name in place of SASHELP and CLASS, respectively (use capitals only).

19 is the number of observations in SASHELP.CLASS.


@Klauth wrote:

Hello Suryakiran, 

 

Thanks for your response. I used the whole thing you wrote. The result was 19. Would I need to chang anything? I have 45 observations in my dataset. 

 

Thanks. 

 

Bo Klauth


 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 8

Re: %SYSFUNC open

Posted in reply to SuryaKiran

Never mind. I got it to work. Thanks. 

 

 

Super User
Posts: 2,073

Re: %SYSFUNC open

[ Edited ]

have you tried to close with proc iml close statement? just a random crazy thought of mine

 

proc iml;

close yourdataset;  

 hmm , by any chance an admin related issue after all? Smiley Sad

Occasional Contributor
Posts: 8

Re: %SYSFUNC open

Posted in reply to novinosrin

Hello, 

 

It tried the code you gave me, but below was the message I got. RC4 is my dataset.

 

ERROR: You cannot open WORK.RC4.DATA for output access with member-level control because
WORK.RC4.DATA is in use by you in resource environment DMS Process.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

 

Thanks. 

Contributor
Posts: 38

Re: %SYSFUNC open

Hi Klauth,

 

     You are getting this error most likely because you have the dataset open. Close and dataset you have open and rerun the code, you should no longer have this error.

 

Thanks

Respected Advisor
Posts: 3,867

Re: %SYSFUNC open


@Klauth wrote:

Hello all, 

 

I used two lines to open a dataset in my macro. 

 

%let dataset=temp;

%let nobs=%sysfunc(attrn(%sysfunc(open(&ds)),NOBS));

 

I used the sysfunc close in order to close, but I learned that my dataset was not closed because I couldn't make changes to the dataset.

 

Can anyone tell me what I can do to close the dataset?

 

Thanks. 

 

Bo klauth


Why do you need to know the number of observations?  What do you do with the number? 

Super User
Posts: 13,946

Re: %SYSFUNC open


@Klauth wrote:

Hello all, 

 

I used two lines to open a dataset in my macro. 

 

%let dataset=temp;

%let nobs=%sysfunc(attrn(%sysfunc(open(&ds)),NOBS));

 

I used the sysfunc close in order to close, but I learned that my dataset was not closed because I couldn't make changes to the dataset.

 

Can anyone tell me what I can do to close the dataset?

 

Thanks. 

 

Bo klauth


You have to have the identifier of which file that has been opened you want to close. You do not show how you attempted to close it.

 

Instead of nesting function calls opening the dataset you are likely better off to use something like:

%let dsid  = %sysfunc(open(&ds, i));

%let nobs = %sysfunc(attrn(&dsid));

%let rc      = %sysfunc(close(&dsid));

 

 

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 13 replies
  • 278 views
  • 3 likes
  • 7 in conversation