BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
harshpatel
Quartz | Level 8
Hello folks, I have a sas data set name as a test If I have a test data set available then it should be call same table If table doesn't exist then I want to create the blank table with some columns
Thanks
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@harshpatel wrote:
Hello folks,
I have a sas data set name as a test
If I have a test data set available then it should be call same table
If table doesn't exist then also I want to call the blank table like test
Thanks

It is not clear what you mean by "call".  Or for that matter "table".

 

Are you saying that if a dataset does not exist you want to create an empty one with that name?
Do you know the structure it should have?

For example do you an existing dataset that you know will always exist that has the right structure?

 

If you have the name of the dataset in a macro variable, say DSNAME, and you have the template dataset available as TEMPLATE then do something like:

%let dsname=MYDS;
%if not %sysfunc(exist(&dsname)) %then %do;
data &dsname;
   stop;
   set template ;
run;
%end;

 to create a new dataset with zero observations.

View solution in original post

8 REPLIES 8
ballardw
Super User

Details when programming are important. Such as are the variables to be create the same every time that Test does not exist has to be used? Do they have the same properties of type(numeric or character), length (especially for character variables)?

 

 

 

harshpatel
Quartz | Level 8
It could be any numeric or character
Basically I need to create blank table with some columns if table doesn't exist
Reeza
Super User

This macro prints a table if the table is empty.

You can modify the code, to remove the proc print and put your create table statements there instead.

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p011imau3tm4jen1us2a45cyenz9.htm

 

data one;
x=1;
run;
data two;
stop;
run;

%macro drive(dsn);
%let dsid=%sysfunc(open(&dsn));
    
 %if &dsid ne 0 %then %do;
  %let cnt=%sysfunc(attrn(&dsid,nlobs));
  %let rc=%sysfunc(close(&dsid));
   %if &cnt ne 0 %then %do;

/*What to do if data set exists*/
    proc print data=&dsn;
     title "This is data from data set &dsn"; 
    run;
   %end;
   %else %do;
/*What to do if data set does not exist*/
    data _null_;
     title;
     file print;
     put _page_;
     put "Data set &dsn is empty.";
    run;
   %end;
 %end;
 %else %put &dsn cannot be open.;
   
%mend drive;
%drive(one)
%drive(two)

@harshpatel wrote:
Hello folks, I have a sas data set name as a test If I have a test data set available then it should be call same table If table doesn't exist then I want to create the blank table with some columns
Thanks

 

Tom
Super User Tom
Super User

This macro prints a table if the table is empty.

Huh?  You appear to using table in two different meanings in the same sentence.

 

Are you saying it makes a report if the dataset is empty?

harshpatel
Quartz | Level 8
Hello folks,
I have a sas data set name as a test
If I have a test data set available then it should be call same table
If table doesn't exist then also I want to call the blank table like test
Thanks
Tom
Super User Tom
Super User

@harshpatel wrote:
Hello folks,
I have a sas data set name as a test
If I have a test data set available then it should be call same table
If table doesn't exist then also I want to call the blank table like test
Thanks

It is not clear what you mean by "call".  Or for that matter "table".

 

Are you saying that if a dataset does not exist you want to create an empty one with that name?
Do you know the structure it should have?

For example do you an existing dataset that you know will always exist that has the right structure?

 

If you have the name of the dataset in a macro variable, say DSNAME, and you have the template dataset available as TEMPLATE then do something like:

%let dsname=MYDS;
%if not %sysfunc(exist(&dsname)) %then %do;
data &dsname;
   stop;
   set template ;
run;
%end;

 to create a new dataset with zero observations.

JOL
SAS Employee JOL
SAS Employee

Slightly different approach

/* create sample test */
data test;
set sashelp.class;
run;

%macro createtable(tab);
%if %sysfunc(exist(&tab))=1 %then /* check if table exists */
%do;

data &tab;
set &tab;
run;

%end;
%else
%do; /*if table does not exist create a generic table, adjust to your needs*/

data new;
length var1 - var5 $ 10;
length var6 - var10 8;
do i = 1 to 5;
output;
end;
drop i;
run;

%end;
%mend createtable;

options mprint;

%createtable(test)
%createtable(test2)

harshpatel
Quartz | Level 8
Thanks, got the solution

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1294 views
  • 0 likes
  • 5 in conversation