Sas - Creating a Dataset Regardless of Input Dataset

I dont really know how to word my question but here it goes. I would like to find out if it is possible in SAS to create the desired dataset even if the input dataset is missing or doesnt exist.

So if you look at the code below, I want to create the test dataset with the specified attributes even if the dummy input dataset does not exist. Is this possible?

Thanks in advance.

Data test;  
 set dummy;
 label subjid = "Subject ID" 
      name   = "Name"
      age    = "Age";
Run;

4 Answers

Check if the dataset exists, if it does then run the data step, if not then create an empty dataset. It may be easier to create an empty version of the dummy dataset first, then read from one or the other depending if the first one exists.

%macro ds_create(dsn);
%if %sysfunc(exist(&dsn.)) %then %do;
    data test;
    set &dsn.;
    run;
%end;
%else %do;
    data test;
    attrib 
        subjid  length=$20  label="Subject ID"
        name    length=$20  label="Name"
        age     length=8   label="Age";
    stop;
    run;
%end;
%mend ds_create;

%ds_create(dummy);
1

PROC APPEND is another solution here. You do need to define your variable lengths and/or formats.

Data test;
label subjid = "Subject ID" 
      name   = "Name"
      age    = "Age";
length subjID 8 name $20 age 3;
stop;
Run;

proc append base=dummy data=test force;
run;

This will append zero rows to Dummy, and create it if needed. If you're trying to get the labels in, the better way is to do that in PROC DATASETS after this step (as I don't think the labels would be applied if DUMMY already exists).

Keith's answer is a good one. An alternative method for creating the test table:

proc sql;
create table test 
    ( subjid char(20) label="Subject ID" 
        ,name char(20) label="Name"
        ,age num label="Age"
    );
quit;
data TEST;
  attrib SUBJID  length=$20  label="Subject ID"
         NAME    length=$20  label="Name"
         AGE     length=8    label="Age";
  %sysfunc(ifc(%sysfunc(exist(DUMMY)), set DUMMY, stop));
run;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest