Help using Base SAS procedures

Trying to use MEMNAME in order to print all data sets

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 90
Accepted Solution

Trying to use MEMNAME in order to print all data sets

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


Accepted Solutions
Solution
‎05-07-2012 02:09 PM
Super Contributor
Posts: 1,636

Re: Trying to use MEMNAME in order to print all data sets

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;

View solution in original post


All Replies
Respected Advisor
Posts: 4,644

Re: Trying to use MEMNAME in order to print all data sets

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

PG
Frequent Contributor
Posts: 90

Re: Trying to use MEMNAME in order to print all data sets

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

Super Contributor
Posts: 1,636

Re: Trying to use MEMNAME in order to print all data sets

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;

%print

Respected Advisor
Posts: 4,644

Re: Trying to use MEMNAME in order to print all data sets

Super Contributor
Posts: 1,636

Re: Trying to use MEMNAME in order to print all data sets

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.

Frequent Contributor
Posts: 90

Re: Trying to use MEMNAME in order to print all data sets

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

Solution
‎05-07-2012 02:09 PM
Super Contributor
Posts: 1,636

Re: Trying to use MEMNAME in order to print all data sets

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;

Frequent Contributor
Posts: 90

Re: Trying to use MEMNAME in order to print all data sets

Hi Linlin,

This worked perfectly. Smiley Happy

Thanks so much!!

Alisa

Super User
Posts: 5,081

Re: Trying to use MEMNAME in order to print all data sets

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.

Super Contributor
Posts: 1,636

Re: Trying to use MEMNAME in order to print all data sets

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

Super User
Posts: 5,081

Re: Trying to use MEMNAME in order to print all data sets

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.

Super Contributor
Posts: 1,636

Re: Trying to use MEMNAME in order to print all data sets

Thank you! using upcase is very important.

Trusted Advisor
Posts: 1,300

Re: Trying to use MEMNAME in order to print all data sets

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;

Super User
Posts: 5,081

Re: Trying to use MEMNAME in order to print all data sets

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!

☑ This topic is SOLVED.

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

Discussion stats
  • 15 replies
  • 407 views
  • 3 likes
  • 5 in conversation