Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAS - Creating a dataset regardless of input dataset

Tags:

sas

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;
like image 437
Allan Cumming Avatar asked Dec 07 '25 19:12

Allan Cumming


2 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);
like image 168
Longfish Avatar answered Dec 10 '25 20:12

Longfish


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).

like image 23
Joe Avatar answered Dec 10 '25 19:12

Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!