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
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!
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.;
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!
Hi SASjedi,
would you please explain why you used "end=EOF" in your code?
Thank you!
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.
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;
Works when I run it!
can you post the code with "if eof then ....do" ? Thank you!
I used your code using 9.2
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
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;
Thank you very much!
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!
Thank you!
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
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.
Ready to level-up your skills? Choose your own adventure.