BookmarkSubscribeRSS Feed
RW9
Diamond | Level 26 RW9
Diamond | Level 26

"Why" do you need a macro?  Have you created code which does this in Base SAS, if so then provide it, and that can then be put into a macro.  If you just want the number of observations from a dataset, then query the metadata tables (SASHELP.VTABLE, VCOLUMN, or DICTIONARY.TABLES, COLUMS in SQL).  User24feb has given you the code for this type of thing.  

Patrick
Opal | Level 21

@RTelang

You're asking for ready made code without putting in some own work first - and amazingly people in this forum work for you free of charge.

I believe it's now really up-to-you to spend a bit of time to understand the answers and use stuff as people tell you it needs to be used.

If you continue on the path you've taken then I start to think "troll".

RTelang
Fluorite | Level 6
i hav done the prog am trying to find a suitable logic through ur inputs has & seen before i gave my code to debug or to see if its correct. am a student learning sas so more number q's from me, give some time i may provide solution to ur probs Patrick k
user24feb
Barite | Level 11

Try:

 

Data A;
  Do i=1 To 18;
    Output;
  End;
Run;

Proc SQL NoPrint;
  * If a dataset is required use;
  Create Table xx As Select Nobs From Dictionary.Tables 
  Where UpCase(Libname)='WORK' AND UpCase(Memname)='A' ;
  * If you need a macro-variable;
  Select Nobs Into :M_Nobs Trimmed From Dictionary.Tables 
  Where UpCase(Libname)='WORK' AND UpCase(Memname)='A' ;
Quit;
%Put ***&M_Nobs.*;
dcruik
Lapis Lazuli | Level 10

Here's a macro similar to @AbhiD.  If you don't specify a data set in the macro call, it will print to the log saying no data set specified (1st call).  If you specify a data set that does not exist, it will print ot the log saying the data set does not exist (2nd call).  If you specify an existing data set, it will print to the log the number of observations in that data set (3rd call).  I'm not sure what you're looking to do with the number of observations, so you may want to be more specific.  For example, if you want the number of observations in a macro variable or a data set, or if you just want it printed to the log (which is what this will do):

 

%macro nobs(dsn);
%if &dsn= %then %do;
	%put *****************************;
	%put *** No Data Set Specified ***;
	%put *****************************;
%end;

%else %do;
	%let dsid=%sysfunc(open(&dsn));

	%if &dsid=0 %then %do;
		%put *******************************;
		%put *** Data Set Does Not Exist ***;
		%put *******************************;
	%end;

	%else %do;
		%let obs=%sysfunc(attrn(&dsid,nlobs));
		%put ****************************************************;
		%put *** Number of Observations in Data Set is = &obs ***;
		%put ****************************************************;
	%end;
%end;
%mend;

%nobs();
%nobs(data);

data test;
	do i=1 to 10;
	output;
	end;
run;

%nobs(test);

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
  • 19 replies
  • 1719 views
  • 0 likes
  • 7 in conversation