DATA Step, Macro, Functions and more

How to print simply Var names in the log window and Output window?

Accepted Solution Solved
Reply
Contributor
Posts: 35
Accepted Solution

How to print simply Var names in the log window and Output window?

Hi Everyone,

I have a null dataset with 10 variables; i want to print only name of the varibales in log window and also output window.how can we do this one?

Thanks,

John


Accepted Solutions
Solution
‎01-07-2012 09:38 PM
SAS Employee
Posts: 104

How to print simply Var names in the log window and Output window?

John,

This DATA _NULL_ step will print the variable names for the SASHELP.CLASS dataset to the SAS log and to the output window at the same time:

data _null_;
   set sashelp.vcolumn end=EOF;
   where libname='SASHELP' and memname='CLASS';
   file print;
   putlog Name @@;
   put Name @@;
run;

Hope this helps!

View solution in original post


All Replies
PROC Star
Posts: 7,467

How to print simply Var names in the log window and Output window?

Many possibilities.  You could use %put statements to print names to the log and, to print them in the output window, create a file and use proc print to print it.

The names can be easily obtained using proc sql.  e.g.,

proc sql;

   create table want as

      select name

         from dictionary.columns

           where libname="SASHELP" and

             memname="CLASS"

    ;

     select name

           into :varnames separated by ' '

            from dictionary.columns

              where libname="SASHELP" and

                memname="CLASS"

   ;

quit;

proc print data=want;

run;

%put &varnames.;

Solution
‎01-07-2012 09:38 PM
SAS Employee
Posts: 104

How to print simply Var names in the log window and Output window?

John,

This DATA _NULL_ step will print the variable names for the SASHELP.CLASS dataset to the SAS log and to the output window at the same time:

data _null_;
   set sashelp.vcolumn end=EOF;
   where libname='SASHELP' and memname='CLASS';
   file print;
   putlog Name @@;
   put Name @@;
run;

Hope this helps!

Super Contributor
Posts: 1,636

How to print simply Var names in the log window and Output window?

Hi SASjedi,

would you please explain why you used "end=EOF" in your code?

Thank you!

PROC Star
Posts: 7,467

How to print simply Var names in the log window and Output window?

Linlin,  I'm not SASJedi, but think I can answer your question.  Since the proposed code was designed to print variable names to both the output window, and log, it wouldn't make much sense to repeatedly print them for every record.  I would have included obs=1 as the way to do that, and my guess is that SASJedi intended to wrap the put and putlog statements with an if EOF then do; .. end; set of statements.  Not very efficient, since it would require reading all of the records, but would have accomplished the desired task.

Super Contributor
Posts: 1,636

How to print simply Var names in the log window and Output window?

Thank you Art!

It did not work if I add "if EOF then do ... end" to SASjedi's code. 

data _null_;

   set sashelp.vcolumn end=EOF;

   where libname='SASHELP' and memname='CLASS';

   if eof then do;

   file print;

   putlog Name @@;

  put Name @@;

        end;

run;

PROC Star
Posts: 7,467

How to print simply Var names in the log window and Output window?

Works when I run it!

Super Contributor
Posts: 1,636

How to print simply Var names in the log window and Output window?

can you post the code with "if eof then ....do" ?   Thank you!

PROC Star
Posts: 7,467

How to print simply Var names in the log window and Output window?

I used your code using 9.2

Super Contributor
Posts: 1,636

Re: How to print simply Var names in the log window and Output window?

I have 9.3. 

This is my log file:

180  data _null_;

181     set sashelp.vcolumn end=EOF;

182     where libname='SASHELP' and memname='CLASS';

183     if eof then do;

184     file print;

185     putlog Name @@;

186    put Name @@;

187      end;

188  run;

Weight

NOTE: 1 lines were written to file PRINT.

NOTE: There were 5 observations read from the data set SASHELP.VCOLUMN.

      WHERE (libname='SASHELP') and (memname='CLASS');

NOTE: DATA statement used (Total process time):

      real time           1.32 seconds

      cpu time            0.59 seconds

PROC Star
Posts: 7,467

Re: How to print simply Var names in the log window and Output window?

Linlin,

You don't get an error.  If you want all of the variable names, how about:

data _null_;

   set sashelp.vcolumn end=EOF;

   where libname='SASHELP' and memname='CLASS';

   length stuff_to_print $200;

   retain stuff_to_print;

   file print;

   stuff_to_print=catx(' ',stuff_to_print,name);

   if eof then do;

     putlog stuff_to_print;

     put stuff_to_print;

   end;

run;

Super Contributor
Posts: 1,636

Re: How to print simply Var names in the log window and Output window?

Thank you very much!

SAS Employee
Posts: 104

How to print simply Var names in the log window and Output window?

Linlin,

The end=EOF is not required for this code to work.  It was "left over" from some experimentation I did earlier on a different approach.  I was originally using an "accumulator" text variable and a do loop that appended each name to the variable until all the variable names had been read.  That approach turned out to be unnecessarily complex and restrictive.  The max size of a text variable is 32K, and because a variable name can be up to 32 characters long, and a dataset can easily have more than 1000 variables, I decided to abandon that approach.  Instead, I chose to directly print each value as it was read, using PUT and PUTLOG statements with @@ (double trailing @'s) to "hold the line" between iterations of the data step.

You can remove the END=EOF from the code without adversely affecting the results.

Stay SASy!

Super Contributor
Posts: 1,636

How to print simply Var names in the log window and Output window?

Thank you!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 13 replies
  • 1075 views
  • 3 likes
  • 4 in conversation