BookmarkSubscribeRSS Feed
Inp
Obsidian | Level 7 Inp
Obsidian | Level 7
Hi Dear,
I am trying to get the number of observation in a dataset.When I wrote the below code in SAS 8.2 I got the followed error message. When wrote the same code in SAS 9.1 , I don't get any error message. Can any one explain why and let me know how to fix it.

Thanks for your help in advance.


Thanks

Inp
DATA USDATA;
INPUT X $1.;
CARDS;
RUN;

DATA _NULL_;
CALL SYMPUT('NOOBS', NOOBS);
SET USDATA NOBS = NOOBS;
STOP;
RUN;



I receive the following message for SAS 8.2
DATA _NULL_;
CALL SYMPUT('NOOBS', NOOBS);
_____
_____
_____
546
546
546
NOTE 546-185: THE VARIABLE NOOBS MAY BE UNINITIALIZED
7 REPLIES 7
data_null__
Jade | Level 19
The NOTE not ERROR is caused by the numeric to character conversion. I would change as follows and move the STOP up. But what you have still works.

[pre]
data _null_;
call symput('noobs',trim(put(noobs,f8.-l)));
stop;
set usdata(drop=_all_) nobs = noobs;
run;
%put NOTE: noobs=&noobs;
[/pre] Added TRIM


Message was edited by: data _null_;
Inp
Obsidian | Level 7 Inp
Obsidian | Level 7
Thanks so much. can you please explain what is this format f8.-l
data_null__
Jade | Level 19
> Thanks so much. can you please explain what is this
> format f8.-l

F is another name for the standard W.D format. -L means left justify. Could have used left(trim(put(nobs,8.)) would be the same result. You want the value of the macro variable to be stripped of leading or trailing blank in most situations.

I often "argue" on SAS-L that putting NOBS into a macro variable is almost always unnecessary, as most examples where NOBS is then used are really just trying to determine if the data is empty.

Simplistic example:
[pre]
data _null_;
if _n_ eq 1 and eof then do;
/* processing for empty data set */
put 'NOTE: Data is empty';
end;
stop;
set usdata end=eof;
run;
[/pre]
Inp
Obsidian | Level 7 Inp
Obsidian | Level 7
Hi Thanks so much. I really really appriciated for your help Mr _NULL_.

Thanks again.
deleted_user
Not applicable
Another way to get the number of observations is to use SASHELP as follows:

DATA _NULL_;
SET SASHELP.VTABLE;
WHERE LIBNAME EQ ‘WORK’ AND MEMNAME EQ ‘EXAMPLE’;
CALL SYMPUTX(‘NOBS’,NOBS);
PUT “NOTE: Dataset WORK.EXAMPLE has “ NOBS “ observations.”;
RUN;

The SYMPUTX function is new in V9 and avoids having to use TRIM and LEFT functions.

I usually struggle to find documentation about these SASHELP tables, but a search just now found it under ‘Concepts: SQL Procedure’.
LinusH
Tourmaline | Level 20
Another, a more direct way is to use the ATTRN function:

%let dsid = %sysfunc(open(mytable));
%let nobs= %sysfunc(attrn(&DSID,nobs));
%put &NOBS;

/Linus
Data never sleeps
data_null__
Jade | Level 19
Did you forget to close the door?

[pre]
15 %let dsid = %sysfunc(open(mytable));
16 %let nobs= %sysfunc(attrn(&DSID,nobs));
17 %put &NOBS;
1
18 /*%let rc = %sysfunc(close(&dsid));*/
19 %put NOTE: &dsid &rc;
NOTE: 1 0
20 proc sort data=mytable;
21 by x;
22 run;

ERROR: You cannot open WORK.MYTABLE.DATA for output access with member-level control because
WORK.MYTABLE.DATA is in use by you in resource environment SORT.

[/pre]

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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