As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Difference between revisions of "Macro nobs"

From sasCommunity
Jump to: navigation, search
m (added references, polishing categories; page reads: 10,510 times)
(added code for macro value-of)
Line 91: Line 91:
 
%local dsid rc;
 
%local dsid rc;
 
%let  dsid  = %sysfunc( open(&Data.      ));
 
%let  dsid  = %sysfunc( open(&Data.      ));
%let  Value = %sysfunc(attrn(&dsid.,&Nobs.));
+
%let  value = %sysfunc(attrn(&dsid.,&nobs.));
 
%let  rc    = %sysfunc(close(&dsid.      ));
 
%let  rc    = %sysfunc(close(&dsid.      ));
%if  &Testing. %then %put _local_;
+
%if  &testing. %then %put _local_;
%ReturnNobs:&Value.
+
%returnnobs:&value.
%mend Nobs;
+
%mend nobs;
 
</source>
 
</source>
  
== Test Data ==
+
== Macro value_of as function ==
  
 
<source lang="sas">
 
<source lang="sas">
*todo;
+
/*       Name: value_of.sas  Ronald J. Fehd  2015
 +
      location: <UNC>\SAS-site\macros
 +
----------------------------------------------------------------------
 +
Requirements  : description  : given a data set
 +
                              returns the attribute
 +
                purpose      : provide function
 +
----------------------------------------------------------------------
 +
Contexts      : program group: development
 +
                program  type: subroutine
 +
                SAS      type: macro, function
 +
                uses routines: none
 +
----------------------------------------------------------------------
 +
Specifications: input  : required: data set name
 +
                        optional: attribute, default=nobs
 +
                process: check for errors
 +
                        check for exist(data set)
 +
                        open  data set, get attribute
 +
                        return value
 +
                output : value
 +
----------------------------------------------------------------------
 +
Usage Examples:
 +
in open code:
 +
%let data = sashelp.class;
 +
title "data: &data. nobs: %value_of(data = &data.)";
 +
 
 +
see test data for macro array
 +
in macro to provide dimension of macro array
 +
macro parameter: data =
 +
%let          dimxyz = %value_of(data = &data);
 +
%do i = 1 to &dimxyz;
 +
    %local      xyz&i.;
 +
    %end;
 +
%array(name = xyz
 +
      ,data = &data.
 +
      ,var  = <var-name>);
 +
 
 +
Information  : author: Ronald J. Fehd
 +
keywords: attribute, function, macro, nlobs, nobs, nvars,
 +
*** .................................... */
 +
%MACRO value_of
 +
        (data      =
 +
        ,attribute = nobs /* or nlobs or nvars etc. */
 +
        ,testing  = 0
 +
        )
 +
/des = 'site: macro function returns attribute of data set'
 +
/* ** store source /* */
 +
;/* RJF 6/2/2015 for sas community organization wiki
 +
**************************************** */
 +
%let testing = %eval(not (0 eq &testing)
 +
            or (    %sysfunc(getoption(echoauto)) NE ECHOAUTO
 +
                and %sysfunc(getoption(mprint)  ) eq MPRINT
 +
                and %sysfunc(getoption(source2) ) eq SOURCE2
 +
                and %sysfunc(getoption(verbose) ) eq VERBOSE));
 +
%local dsid rc value;
 +
%let  value = .;
 +
/***********************************************************
 +
%* no value checking: RTFM;
 +
%if not(  "&attribute" eq "nobs"
 +
        or "&attribute" eq "nlobs")
 +
        or "&attribute" eq "nvars")
 +
        %then %do;
 +
    %put Err%str()or: %SysMacroName Nobs must be in (nobs,nlobs);
 +
    %goto ReturnNobs;
 +
    %end;
 +
%if not(%sysfunc(exist(&data))) %then %do;
 +
    %put &sysmacroname:not exists: &data;
 +
    %goto returnnobs;
 +
    %end;
 +
/**********************************************************/
 +
%let dsid  = %sysfunc( open(&data          ));
 +
%let value = %sysfunc(attrn(&dsid,&attribute));
 +
%let rc    = %sysfunc(close(&dsid          ));
 +
%if  &testing %then %put _local_;
 +
%returnnobs:&value
 +
%mend value_of;
 
</source>
 
</source>
  
'''Log:'''
+
== Test Data ==
 +
 
 +
<source lang="sas">
 +
%let nobs = %nobs(data=sashelp.class);
 +
%put &=nobs;
 +
%let nobs = %value_of(data=sashelp.class);
 +
%let nvars = %value_of(data=sashelp.class
 +
                      ,attribute=nvars);
 +
%put &=nobs &=nvars;
 +
</source>
  
 
== References ==
 
== References ==
Line 120: Line 203:
 
-- created by [[User:Rjf2]]  15:46, 12 June 2012 (EDT)
 
-- created by [[User:Rjf2]]  15:46, 12 June 2012 (EDT)
  
--[[User:Ron.Fehd.macro.maven|Ronald_J._Fehd macro.maven == the radical programmer]] 19:44, 27 June 2012 (EDT)
+
--[[User:Ron.Fehd.macro.maven|Ronald_J._Fehd macro.maven == the radical programmer]] 6/2/2015 11:01:28 AM (EDT)
  
 
[[Category:%25SYSFUNC_Function]]
 
[[Category:%25SYSFUNC_Function]]

Revision as of 10:17, 2 June 2015

Q: How can I get the number of observations of a data set, without opening the data set?

A: Use the macro Nobs function.


I am definitely not The.Original author of a macro nobs as a macro function.

See the references for previous versions of macros named Nobs.

Author: Ronald_J._Fehd

Macro Nobs as function

 /*       Name: nobs.sas   Ronald J. Fehd  2007
      location: <UNC>\SAS-site\macros
----------------------------------------------------------------------
Requirements  : description  : given a data set
                               returns the number of observations
                purpose      : provide function
----------------------------------------------------------------------
Contexts      : program group: development
                program  type: subroutine
                SAS      type: macro, function
                uses routines: none
----------------------------------------------------------------------
Specifications: input  : required: data set name
                         optional: name of attribute: in (nobs, nlobs)
                process: check for errors
                         check for exist(data set)
                         open  data set, get attribute
                         return value
                output : value
----------------------------------------------------------------------
Usage Examples:
in open code:
%Let Data = sashelp.class;
title "data: &Data. nobs: %nobs(data = &Data.)";
 
see test data for macro array
in macro to provide dimension of macro array
macro parameter: data =
%let          DimXyz = %nobs(data = &Data.);
%do I = 1 to &DimXyz;
    %local       Xyz&I.;
    %end;
%Array(Name = Xyz
      ,Data = &Data.
      ,Var  = <Var-Name>);
 
Information   : author: Ronald J. Fehd
keywords: attribute, function, macro, nlobs, nobs, scl functions
*** .................................... */
%MACRO nobs
     (Data    =
     ,Nobs    = nobs /* or nlobs */
     ,Testing = 0
     )
/ des  = 'site: macro function returns nobs | nlobs of data set'
 /* except dictionary.??? */
 /* ** store source /* */
;/* 3/21/2007 RJF2
NOTE: see attrn(...,nlobs)
      obs not marked for deletion
Error: NOBS: not exists: dictionary.<???>
todo: test sashelp.v???
**************************************** */
%let Testing = %eval(    &Testing.
                     or (    %sysfunc(getoption(ECHOAUTO)) NE ECHOAUTO
                         and %sysfunc(getoption(MPRINT)  ) eq MPRINT
                         and %sysfunc(getoption(SOURCE2) ) eq SOURCE2
                         and %sysfunc(getoption(VERBOSE) ) eq VERBOSE));
%local Value;
%let   Value = .;
%let   Nobs  = %lowcase(&Nobs.);
%if not(   "&Nobs" eq "nobs"
        or "&Nobs" eq "nlobs") %then %do;
    %put Err%str()or: %SysMacroName Nobs must be in (nobs,nlobs);
    %goto ReturnNobs;
    %end;
%if %index(&Data.,.) %then %do;
    %let Libref = %lowcase(%scan(&Data.,1,.));
    %if "&Libref." eq "dictionary" %then
        %put Err%str()or: &SysMacroname.: libref cannot be &Libref.;
    %goto ReturnNobs;
    %end;
%if not(%sysfunc(exist(&Data.))) %then %do;
    %put &SysMacroname.:not exists: &Data.;
    %goto ReturnNobs;
    %end;
%local dsid rc;
%let   dsid  = %sysfunc( open(&Data.       ));
%let   value = %sysfunc(attrn(&dsid.,&nobs.));
%let   rc    = %sysfunc(close(&dsid.       ));
%if   &testing. %then %put _local_;
%returnnobs:&value.
%mend nobs;

Macro value_of as function

 /*       Name: value_of.sas   Ronald J. Fehd  2015
      location: <UNC>\SAS-site\macros
----------------------------------------------------------------------
Requirements  : description  : given a data set
                               returns the attribute
                purpose      : provide function
----------------------------------------------------------------------
Contexts      : program group: development
                program  type: subroutine
                SAS      type: macro, function
                uses routines: none
----------------------------------------------------------------------
Specifications: input  : required: data set name
                         optional: attribute, default=nobs
                process: check for errors
                         check for exist(data set)
                         open  data set, get attribute
                         return value
                output : value
----------------------------------------------------------------------
Usage Examples:
in open code:
%let data = sashelp.class;
title "data: &data. nobs: %value_of(data = &data.)";
 
see test data for macro array
in macro to provide dimension of macro array
macro parameter: data =
%let          dimxyz = %value_of(data = &data);
%do i = 1 to &dimxyz;
    %local       xyz&i.;
    %end;
%array(name = xyz
      ,data = &data.
      ,var  = <var-name>);
 
Information   : author: Ronald J. Fehd
keywords: attribute, function, macro, nlobs, nobs, nvars,
*** .................................... */
%MACRO value_of
         (data      =
         ,attribute = nobs /* or nlobs or nvars etc. */
         ,testing   = 0
         )
/des = 'site: macro function returns attribute of data set'
 /* ** store source /* */
;/* RJF 6/2/2015 for sas community organization wiki
**************************************** */
%let testing = %eval(not (0 eq &testing)
             or (    %sysfunc(getoption(echoauto)) NE ECHOAUTO
                 and %sysfunc(getoption(mprint)  ) eq MPRINT
                 and %sysfunc(getoption(source2) ) eq SOURCE2
                 and %sysfunc(getoption(verbose) ) eq VERBOSE));
%local dsid rc value;
%let   value = .;
/***********************************************************
%* no value checking: RTFM;
%if not(   "&attribute" eq "nobs"
        or "&attribute" eq "nlobs")
        or "&attribute" eq "nvars")
        %then %do;
    %put Err%str()or: %SysMacroName Nobs must be in (nobs,nlobs);
    %goto ReturnNobs;
    %end;
%if not(%sysfunc(exist(&data))) %then %do;
    %put &sysmacroname:not exists: &data;
    %goto returnnobs;
    %end;
/**********************************************************/
%let dsid  = %sysfunc( open(&data           ));
%let value = %sysfunc(attrn(&dsid,&attribute));
%let rc    = %sysfunc(close(&dsid           ));
%if  &testing %then %put _local_;
%returnnobs:&value
%mend value_of;

Test Data

%let nobs = %nobs(data=sashelp.class);
%put &=nobs;
%let nobs = %value_of(data=sashelp.class);
%let nvars = %value_of(data=sashelp.class
                      ,attribute=nvars);
%put &=nobs &=nvars;

References

This macro has appeared in many of my previous publications:

-- created by User:Rjf2 15:46, 12 June 2012 (EDT)

--Ronald_J._Fehd macro.maven == the radical programmer 6/2/2015 11:01:28 AM (EDT)