BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Klauth
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
SuryaKiran
Meteorite | Level 14

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

13 REPLIES 13
SuryaKiran
Meteorite | Level 14

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
Klauth
Fluorite | Level 6

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

SuryaKiran
Meteorite | Level 14

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
Klauth
Fluorite | Level 6

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. 

ybolduc
Quartz | Level 8

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

Klauth
Fluorite | Level 6

Thanks. 

Kurt_Bremser
Super User

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


 

Klauth
Fluorite | Level 6

Never mind. I got it to work. Thanks. 

 

 

novinosrin
Tourmaline | Level 20

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

Klauth
Fluorite | Level 6

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. 

ybolduc
Quartz | Level 8

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

data_null__
Jade | Level 19

@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? 

ballardw
Super User

@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));

 

 

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 13 replies
  • 3033 views
  • 3 likes
  • 7 in conversation