BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Paul628
Calcite | Level 5

Hello all,

 

I have the following code and my goal is to have a txt. export with a header that includes the number of rows/obs from my dataset.

 

 

DATA have;
SET have END=EOF;
file "filepath.txt" dlm='|';

put (_all_)(~);
IF EOF then 
	do;
		put 'Number of Rows:'_n_:Z7.;		
	end;
run;

 

 

As it is now, the only way I could get _n_ to include a count of rows/obs was to put it at the end. When I used:

if _n_>0 then

do;

put 'Number of Rows:'_n_:z7.;

end;

run;

 

then the header was at the top of the file, but _n_ = 1.

 

Any help is much appreciated.

 

Thank you.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
TomKari
Onyx | Level 15
proc sql noprint;
	select nobs into :rowcount
		from dictionary.tables
			where upcase(libname) = "SASHELP" and upcase(memname) = "CLASS";
quit;

data _null_;
	SET sashelp.class;
	file "!USERPROFILE\Desktop\want.txt" dlm='|';

	if _n_ = 1 then
		put "Number of Rows: &rowcount.";
	put (_all_)(~);
run;

View solution in original post

3 REPLIES 3
TomKari
Onyx | Level 15
proc sql noprint;
	select nobs into :rowcount
		from dictionary.tables
			where upcase(libname) = "SASHELP" and upcase(memname) = "CLASS";
quit;

data _null_;
	SET sashelp.class;
	file "!USERPROFILE\Desktop\want.txt" dlm='|';

	if _n_ = 1 then
		put "Number of Rows: &rowcount.";
	put (_all_)(~);
run;
Paul628
Calcite | Level 5

Worked like a charm!

 

Thank you,

 

Paul

svh
Lapis Lazuli | Level 10 svh
Lapis Lazuli | Level 10

One way--maybe not the best way--is to use CALL SYMPUTX in a prior step. This way, you can store the total number of observations in a macro variable that you call when you export the text file. In this example, the number of observations is printed above the list of values. I'm not sure if this is exactly what you're looking for, but it may help. 

 

data have;
input x @@;
datalines;
1 2 3 4 5 6 7 8 9 0 
;
run;
proc print;
run;

data _null_;
   set have;
   total = _n_ ;
   call symputx("macro_total", total);
   run; 
data _null_;
	set have;
	file "c\file.txt";
	if _n_ = 1 then do;
	   put "&macro_total";
	   end;
    put x;
	run;

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 3 replies
  • 3434 views
  • 1 like
  • 3 in conversation