The purpose of this SAS example is to show how to add an ‘auto’ increment numeric variable to a SAS dataset with only character variables so we can find out the number of observations (N) in the dataset.  Not only that but to have SAS export a text file that includes the new dataset name and dynamically include the number of observations in the file name and within the text file itself.  The tricky part was getting the N value in the filename and dynamically add it to the text file.  For this example it assumes you will create a directory named J:\SaveMyFilesHere\output for your text file with number of observations.

/* temp SAS repository*/
libname TEMPREP 'J:/SAS_queries';/* main SAS dataset repository*/
libname SASREP 'J:/SAS_repository';

/* simply pull some variables from a dataset (SASREP .DS942) and place in a new dataset named TEMPREP.ds942_dpn_823 */
PROC SQL; CREATE TABLE TEMPREP.ds942_dpn_823 AS SELECT t942.subject, t942.q16L, t942.q16R, t942.study_id FROM SASREP .DS942 AS t942 WHERE t942.study_id=1;

* add an auto increment variable (sas_incr_wpg) since all of the variables we are pulling are character variables and we need a numeric variable to get the number of observations for the TEMPREP.ds942_dpn_823 dataset;
DATA TEMPREP.ds942_dpn_823;
SET TEMPREP.ds942_dpn_823;
sas_incr_wpg = _N_;   * you can name this variable (sas_incr_wpg) what every you want but I wanted to name it something that would not appear as a variable name in any of my other datasets in my SAS repository;


/* Macro code:

@abstract Export the number of observations for a dataset.
@attribute 'datasrc' - is simply the name of the dataset

%macro exportobs(datasrc,tmpobsds,outpath,incvarname);

* now we set the N result number to a variable we can use and actually include the N value in the file name;
* we use the z8. format for the number otherwise we will have blanks at the beginning of the number (which leads to blanks in our file name which we probably do not want);
proc sql NOPRINT;
select put(COUNT(&incvarname),z8.) into :sas_incr_wpg_N
from &datasrc;

* dynamically build the file path and file name and add the number of observations to the actual file name (remove '\output' from the string if you do not want the file to go under a subdirectory named 'output');
%let newFile = &outpath.\output\&datasrc._N-&sas_incr_wpg_N..txt;

* thanks to the following reference for tips on how to hack the data step (;
* ---- begin data step hack ----;
filename temp temp;

data _null_;
file temp;

* create a dataset with the number of observations from our main dataset;
LENGTH N_observations 8.;    * adds an observation column with a numeric value with a length of 8;
infile temp;
input @;
* add the number of observations to the dataset;
do _infile_=
input N_observations @;

filename temp clear;
* ---- end data step hack ----;

* export the observation stats text file;
proc export data=TEMPREP.NULL
outfile="&newFile" replace
delimiter='    ';
%mend exportobs;

* now we run the macro with the above data query ;
%exportobs(TEMPREP.ds942_dpn_823, TEMPREP.N_OBS_ds942_dpn_823, J:\SaveMyFilesHere, sas_incr_wpg);