BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DmytroYermak
Lapis Lazuli | Level 10

Hi all,

 

It is the macro to reveal the number of observations in a dataset:

 

 

%let nobs=%sysfunc(attrn(%sysfunc(open(comp_more_one_1)),nlobs));
%put nobs= &nobs ;

 

Why should the opened dataset be closed further:

 

 

%let dsid=%sysfunc(close(%sysfunc(open(comp_more_one_1))));

?

 

Thank you!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

When you open a file, you lock it for your own use.

It remains locked until you close the handle.

 

Also note that the code you show is not correct. Do no try to aggregate open with other functions.

Use:

1. open

2. attrn

3. close

in separate steps. Follow the documentation.

 

View solution in original post

5 REPLIES 5
vkumbhakarna
Fluorite | Level 6

I have faced weird errors if a file was not closed, especially if it is further utilized in the process, therefore it is advisable to close the file explicitly using the close command. Per SAS documentationy default, a file is opened with a control level of RECORD.  OPEN defaults to the strongest access mode available in the engine. That is, if the engine supports random access, OPEN defaults to random access. Otherwise, files are opened with sequential access, and a system-level warning is set. If you do not close the file, it remains in the memory and may cause issues if it is utilized again in the same macro, but  If you open a file within a DATA step, it closes automatically when the DATA step ends. 

Hope this helps!

ChrisNZ
Tourmaline | Level 20

When you open a file, you lock it for your own use.

It remains locked until you close the handle.

 

Also note that the code you show is not correct. Do no try to aggregate open with other functions.

Use:

1. open

2. attrn

3. close

in separate steps. Follow the documentation.

 

DmytroYermak
Lapis Lazuli | Level 10
Thank you!
SASKiwi
PROC Star

Try this for yourself:

32         %let nobs=%sysfunc(attrn(%sysfunc(open(work.class)),nlobs));
33         %put nobs= &nobs ;
nobs= 19
34         
35         data class;
36           set class;
37           if age < 12;
38         run;

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

As @ChrisNZ says, the OPEN function creates an OS lock on the file the dataset is stored in. You will be unable to write to this SAS dataset until you release the lock.

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
  • 5 replies
  • 780 views
  • 8 likes
  • 5 in conversation