Hello Everyone,
So I have this problem on my homework assignment where I need to write a proc print statement using sashelp.vcolumns dictionary to print all of the datasets that contain Product_ID.
This is my code so far:
data _null_;
set sashelp.vcolumn;
where libname='ORION' and name='Product_ID';
file 'C:\SAS\print_products.sas';
put 'proc print data=orion.memname (obs=5);';
put 'run;';
run;
%include 'C:\SAS\print_products.sas' /source2;
Unfortunately, so far I am not successful with this code and I know it's because I am not not familiar with how to use memname.
Could someone please take a look at my code and let me know where I am going wrong?
Thanks,
Alisa
Alisa,
I tried your code and it worked perfectly after unquote memname. Please try the code below:
data _null_;
set sashelp.vcolumn;
where libname='ORION' and name='Product_ID';
file 'C:\SAS\print_products.sas';
put 'proc print data=orion.' memname '(obs=5);';
put 'run;';
run;
%include 'C:\SAS\print_products.sas' /source2;
Well, memname is a variable in dataview sashelp.vcolumn. In your put statement, the word memname is simply a part of a string. It is not going to be replaced by the value of memname (the name of the dataset). Investigate how the put statement could write the value of memnage instead of the word memname itself.
Good luck.
PG
Hello PG,
Do you have any suggestions on where I could start to look for that information?
I have tried to search on Google and within the SAS help docs and I have not come up with anything.
Thanks,
Alisa
Hi Alisa,
If you don't have to use " put", you can try my code.
proc sql noprint;
select memname into :dsn separated by ' '
from sashelp.vcolumn
where libname='ORION' and name='Product_ID';
quit;
%put &dsn;
%macro print;
%do i=1 %to %sysfunc(countw(&dsn));
proc print data=orion.%scan(&dsn,&i)(obs=5);
run;
%end;
%mend;
Look at the description of the put statement in
PG
Thank you PG for the info!
Alisa, first read the link PG provided. If you can't figure out after reading, then use " put 'proc print data=orion.'memname '(obs=5);';" to modify your code.
Hi Linlin,
This did get me a little bit further in my code, but I kept getting the following error:
225 +proc print data=orion."memname" (obs=5);
---------
22
76
ERROR: Invalid data set name orion..
ERROR 22-322: Syntax error, expecting one of the following: ;, (, BLANKLINE, DATA, DOUBLE,
HEADING, LABEL, N, NOOBS, OBS, ROUND, ROWS, SPLIT, STYLE, SUMLABEL, UNIFORM,
WIDTH.
ERROR 76-322: Syntax error, statement will be ignored.
226 +run;
If you have any suggestions on how to improve this, that would be great.
Thanks,
Alisa
Alisa,
I tried your code and it worked perfectly after unquote memname. Please try the code below:
data _null_;
set sashelp.vcolumn;
where libname='ORION' and name='Product_ID';
file 'C:\SAS\print_products.sas';
put 'proc print data=orion.' memname '(obs=5);';
put 'run;';
run;
%include 'C:\SAS\print_products.sas' /source2;
Hi Linlin,
This worked perfectly.
Thanks so much!!
Alisa
Alisa,
First, let's see if we are answering the right question. Which of these do you need to do? From the ORION library:
1. Print the name of every data set that contain Product_ID?
2. Print the first 5 observations from every data set that contains Product_ID?
3. Print some other information about every data set that contains Product_ID?
Once the question is clear, we can patch up the solution. It looks like you have been trying to solve question 2, but I'm not convinced that is the right thing to do.
Hi Astounding,
This is how I tested Alisa's code. I think it solved "2. Print the first 5 observations from every data set that contains Product_ID?".
data test1 test2 test3;
do i=1 to 8;
output ;
end;
run;
data _null_;
set sashelp.vcolumn;
where libname='WORK' and name='i';
file 'C:\temp\forum\print_products.sas';
put 'proc print data=work.' memname '(obs=5);';
put 'run;';
run;
%include 'C:\temp\forum\print_products.sas' /source2;
Linlin
Linlin,
If question 2 is the right one, your code is a good start. The only change I would suggest is in the WHERE statement:
where libname='WORK' and upcase(name)='I';
It is possible that Product_ID takes on a variety of capitalizations in various data sets. The code you posted will run without error, but that might be deceptive. It is possible that there would be additional data sets that contain PRODUCT_ID, for example.
If one of the other questions is the right one, the program becomes even simpler. But I would wait to hear from Alisa before trying to change the question.
Message was edited: One more change I forgot to mention: I would add a PUT statement to write a TITLE statement that contains the name of the data set being printed. As it stands, that's key information that is not included in the report.
Thank you! using upcase is very important.
data _null_;
set sashelp.vcolumn;
where libname='WORK' and upcase(name)='NAME';
call execute('title "' || catx('.',libname,memname) || '";');
call execute('proc print data=' || catx('.',libname,memname) || '(obs=5); run;');
run;
FriedEgg,
In a way, you are absolutely right. I would have done it something like that, except that I'm working with an older version of SAS that doesn't have the CAT functions. (That will change later this month. yay!) But there are ways around using the CAT functions.
However ...
The original poster was having trouble getting the proper syntax for a PUT statement. I wouldn't add the burden of learning CALL EXECUTE under those conditions. While I think your solution is better, I try to stick to solutions that the poster will understand, even if I think another solution is better. All that being said, I wouldn't be surprised if the actual question should have been different and the answer should have been:
proc print data=sashelp.vcolumn;
where libname='ORION' and upcase(name) = 'PRODUCT_ID';
run;
Thanks!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.