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!
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.
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!
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.
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.
On top of what the others said, you accumulate file handles, leading to a "memory leak", or an overflow of system limits (number of concurrently open files).
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.