BookmarkSubscribeRSS Feed
Lucas
Calcite | Level 5

Hi,

I'm trying to create a view with parameterized list of tables. View definition must be create once and must be automatically refreshed.

List of tables is keep in a data set which can be edit by the user.


I was trying to create a macro in autocall library and use it in view definition. Like this:

%macro table;

      sashelp.class sashelp.class

%mend;

data v_table / view=v_table;

      set %table;

run;

but I don't know how to create table list based on data set:


data tables;

   input table_nm $32.;

   datalines;

sashelp.class

sashelp.class

;

%macro table;

      <query to the 'tables' data set >

%mend;

data v_table / view=v_table;

      set %table;

run;

Maybe there is an easier way ?

8 REPLIES 8
data_null__
Jade | Level 19

How about DOSUBL?  You can also make %TABLE have a parameter to subset the list of table names.

proc datasets kill;
  
run;

data tables;
   input table_nm $32.;
  
datalines;
sashelp.class
sashelp.class
;
 
%macro table;
  
%local x rc tablelist;
   %let x=%str(proc sql; select table_nm into :tablelist separated by ' ' from tables; quit; run;);
  
%let rc=%sysfunc(dosubl(&x));
   &tablelist
%mend;

data v_table / view=v_table;
   set %table;
  
run;

proc print data=v_table;
   run;
ballardw
Super User

I'm not sure what you are meaning by a "table list" in this instance. Please provide a bit more detail.

Are you attempting to create a view that automatically updates the description of the data view from the descriptions in the tables dataset so that the view definition changes when items are added to the table without rerunning the view descriptor?

I don't think that would work as the datasets described at the time the descriptor code (the data v_table / view=v_table;)

is what will be in the view regardless of changes to the values in the tables data set. You would have to do something like

%macro table;

proc sql noprint;

   select table_nm into : tablelist separated by ' '

   from tables

;

quit;

data v_table / view=v_table;

      set &tablelist;

run;

%mend;

And use

%table;

To make this more general you would add parameters to have 1) the table to read for the list, 2) the name of the output view and 3) possibly the variable name in the table to use.

Tom
Super User Tom
Super User

If all of the possible tables exist and you just want to pass in which ones to read then you could do it with a data step view.  But all of the tables need to exist when the view is defined and when it is run.  (see below)  But you are probably better off using macro logic (or macro variable) to define the view/dataset based on the list from the table at run time.

data table1 table2 table3 ;

  set sashelp.class ;

run;

data my_tables (index=(dsname));

  input dsname $41. ;

  dsname=upcase(dsname);

cards;

work.table1

work.table2

;;;;

data all / view=all ;

  length dsname dsname2 $41 ;

  set work.table: indsname=dsname2 ;

  dsname=dsname2;

  set my_tables key=dsname /unique ;

  if dsname2 ne lag(dsname2) then put dsname2= _n_= obsnum= _iorc_=;

  if not _iorc_ then do; obsnum+1; output; end;

  _error_=0;

  _iorc_=0;

run;

data _null_;

if eof then put (_n_ obsnum) (=);

set all end=eof ;

run;

Lucas
Calcite | Level 5

Thank you for all responses. I have read more about views and I think there is no way to achieve what I want.

I will provide more details about my problem. I think now it will be easier to understand.

There is a process that creates a table once a moth:

INCOMES_201410

INCOMES_201411

INCOMES_201412

INCOMES_201501

INCOMES_201502

I need to create a view which provides data from all tables INCOMES_*, but I don't want to change definition of the view each month.

I was hoping there is a way to create a view once and when someone use this view it automatically check which tables to use.

ballardw
Super User

You could create your own batch job that checks daily, or as appropriate, to see if that file has been updated and then run a SAS job whose only purpose is to update those definitions. Ideal would be to piggy-back on the other process to call the SAS job at the end but I realize that may not be practical.

Tom
Super User Tom
Super User

Why?

1) Why do you want a view?  Why not just have a macro or some other piece of dynamic code?

2) Why NOT just re-define the view?  You are already running a program to create the new dataset.  It is a trivial extra step to have that program define the view.

Jbabu
Calcite | Level 5

HI

i have list of varibles how to make create macros list

_0  _1 _2   to _200

how can i make this list in macros because its included   '_'  its getting confustion to create list

and again i have convert to char to numeric :

  Proc sql ;

   select name into :vars separated by ' ' from  sample;

  quit;

in above code how can i declare a range instead of using name  what can i declare my numbers range can any one clarify on this

  %let Novars =%sysfunc(countw(&vars)) ;

  %do i = 1 % to &Novars ;

    %local &&Ovar&i ;

  %let Ovar&i = %scan(&vars , &i ) ;

  //%let Finavar&i = "_"||strip(&&Ovar&i) ;

  %end l

data sample 1;

   set sample ;

   %do i = 1 % 19 ;

    &&var&i = input(&&Finavar&i,best12.);

   %end;   

   drop _000-_019;

run;

Tom
Super User Tom
Super User

Please make a new question thread for this as it does not seem to be related to the original question.

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
  • 8 replies
  • 1373 views
  • 0 likes
  • 5 in conversation