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

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

1 ACCEPTED SOLUTION

Accepted Solutions
SASJedi
SAS Super FREQ

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!

Check out my Jedi SAS Tricks for SAS Users

View solution in original post

13 REPLIES 13
art297
Opal | Level 21

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.;

SASJedi
SAS Super FREQ

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!

Check out my Jedi SAS Tricks for SAS Users
Linlin
Lapis Lazuli | Level 10

Hi SASjedi,

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

Thank you!

art297
Opal | Level 21

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.

Linlin
Lapis Lazuli | Level 10

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;

art297
Opal | Level 21

Works when I run it!

Linlin
Lapis Lazuli | Level 10

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

art297
Opal | Level 21

I used your code using 9.2

Linlin
Lapis Lazuli | Level 10

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

art297
Opal | Level 21

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;

Linlin
Lapis Lazuli | Level 10

Thank you very much!

SASJedi
SAS Super FREQ

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!

Check out my Jedi SAS Tricks for SAS Users

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!

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
  • 13 replies
  • 6779 views
  • 3 likes
  • 4 in conversation