Search code examples
sassas-macro

load and combine all SAS dataset


I have multiple SAS dataset in single location(folder) with two columns and name of the SAS dataset seems to be Diagnosis_<diagnosis_name>.

Here I want to load all dataset and combine all together like below,

Sample data set

File Location: C:\Users\xyz\Desktop\diagnosis\Diagnosis_<diagnosis_name>.sas7bdat

1. Dataset Name : Diagnosis_Diabetes.sas7bdat

enter image description here

2. Dataset Name : Diagnosis_Obesity.sas7bdat

enter image description here

Ouput which I expect like this

enter image description here

Could you please help me on this.


Solution

  • You can just combine the datasets using SET statement. If want all of the datasets with names that start with a constant prefix you can use the : wildcard to make a name list.

    First create a libref to reference the directory:

    libname diag 'C:\Users\xyz\Desktop\diagnosis\';
    

    Then combine the datasets. If the original datasets are sorted by the PersonID then you can add a BY statement and the result will also be sorted.

    data tall;
      set diag.diagnosis_: ;
      by person_id;
    run;
    

    If want to generate that wide dataset you could use PROC TRANSPOSE, but in that case you will need some extra variable to actually transpose.

    data tall;
      set diag.diagnosis_: ;
      by person_id;
      present=1;
    run;
    
    proc transpose data=tall out=want(drop=_name_);
       by person_id;
       id diagnosis;
       var present;
    run;