****************************************************************************************************
*                                           PROGRAM OVERVIEW
****************************************************************************************************
*
* PROGRAM: ms_partition_splitdata.sas  
* Created (mm/dd/yyyy): 08/18/2020
*
*--------------------------------------------------------------------------------------------------
* PURPOSE:
*  This program splits a dataset based on partition number
*
*  Program inputs:
*   - dataset to partition
*
*  Program outputs:                                                                                                                                       
*	- dataset partitioned by patid
* 
*  PARAMETERS:                  
*   - partitiondata: dataset containing patID and partition 
*   - datafile: source data to be split
*   - outfile: output file dataset prefix
*   - num_partitions: number of partitions
*
*  Programming Notes:    
*
*--------------------------------------------------------------------------------------------------
* CONTACT INFO: 
*  Sentinel Coordinating Center
*  info@sentinelsystem.org
*
***************************************************************************************************;

%macro ms_partition_splitdata(partitiondata = , datafile = , outfile =, num_partitions=);

    data %do p = 1 %to &num_partitions.;
           &outfile._&p.(drop=partition)
	  	 %end;;
	   
	   if _n_ = 1 then do;
        declare hash split(DATASET:"&partitiondata.");
        split.defineKey("patid");
        split.defineData('partition');
        split.defineDone();
      end;
	  call missing(partition);

      set &datafile.;
      if split.find()=0;
      %do p = 1 %to &num_partitions.;
         if partition = &p. then output &outfile._&p.;
      %end;
    run;

%mend ms_partition_splitdata;