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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

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

15 REPLIES 15
PGStats
Opal | Level 21

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
InfoAlisaA
Calcite | Level 5

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

Linlin
Lapis Lazuli | Level 10

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

Linlin
Lapis Lazuli | Level 10

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.

InfoAlisaA
Calcite | Level 5

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

Linlin
Lapis Lazuli | Level 10

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;

InfoAlisaA
Calcite | Level 5

Hi Linlin,

This worked perfectly. Smiley Happy

Thanks so much!!

Alisa

Astounding
PROC Star

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.

Linlin
Lapis Lazuli | Level 10

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

Astounding
PROC Star

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.

Linlin
Lapis Lazuli | Level 10

Thank you! using upcase is very important.

FriedEgg
SAS Employee

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;

Astounding
PROC Star

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!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 15 replies
  • 2625 views
  • 3 likes
  • 5 in conversation