Creating Multiple Datasets from Existing Dataset - Different Situations | DYNAMIC SPLIT | USE OF CALL EXECUTE FUNCTION

Creating Multiple Datasets in one go is a Challenge often faced by SAS users, but It’s a Very easy process in SAS. 

There could be Two situations - 

1. Number of Dataset are already known-

Example - 

Student Data we want to split on basis of Gender, so here we already know that we need to create Male and Female, Two New datasets

2. Number of Dataset are not known

Example - 

SASHELP.CARS has N Number of Make in it, we wants to create all datasets for all unique Value in Make Variable, eg - Acura dataset will have all rows for Acura, Audi dataset will have all rows for Audi and so on.

Here Number of Dataset are not known it is based on Unique Value available in Make Variable.

Let's see our First Type where Number of Dataset are already know - 


 /* Created a Student Dataset with Few records */
Data Student;
Input Name $ Age Gender $;
cards;
Alex 20 M
Sady 21 F
Amanda 22 F
John 21 M
Jane 19 F
Mike 18 M
Chris 22 M
Nayasa 20 F
;
run;

/* Split Dataset based on Gender using If Then Output statement */

Data Male Female;
set Student;
if Gender='M' then output Male;
else if Gender='F' then Output Female;
run;

/*Printed all Three Datasets */

Proc Print Data=Student;Run; 
Proc Print Data= Male; Run;  
Proc Print Data= Female; Run; 

Muliple Dataset

Now Let's see our second Type where Number of Dataset are Unknown-

/* First We get the All Unique Value of Make in a New Datset "Unique_Make" */
Proc Sort Data=SAShelp.Cars Nodupkey out=Unique_make (Keep=Make);
by Make;
run;
/*Create a Name Variable in Unique_Make */

Data Unique_make;
set Unique_make;
Name=Scan(Make,1,'-');
run;

/* We created this Name becuase Make has a Value 'Mercedes-Benz', we can't use Mercedes-Benz as Dataset Name,
So here we used SCAN Function to Avoid this kind of Situation otherwise it will error our program. */

/*Now this is Final and important Code where we acually Split SASHELP.CARS into Multiple Datasets based on MAKE */

Data _NULL_;
set unique_Make;
call execute('data ' !! compress(Name) !! '; set sashelp.cars; where Make = "' !! Make !! '"; run;');
run;
Multiple Dataset in one Go


/*Similarly we can use Call Execute Funtion to Print all Datasets in one Go */

Data _NULL_;
set unique_Make;
call execute('Proc Print Data= ' !! compress(Make) !! '; run;');
run; 

Use of Call Execute Function

 

Post a Comment

0 Comments