Hello there. I'm looking for a little help with exporting some data.
I have a data set that contains all records where a variable named "IEE" matches to a top ten count which will change every time its ran. The variable is numeric and always 4 numbers.
Sample Is
ID IEE Var1 Var2
111 3007 Red Tree
222 3007 Red House
333 3007 Blue Car
444 4333 Red Car
555 4333 Red Tree
666 4333 Red Tree
777 5544 Red House
888 5544 Red House
What i'm trying to achieve is I want the code to automatically create a separate dataset for each of the values in variable IEE so while i have a top 10 if we were to run it on the above sample we would have 3 datasets.
one for 3007 containing 3 observations
one for 4333 containing 3 observations
one for 5544 containing 2 observations
I'm not too fussed what the output datasets are called as long as they're identifiable (probably sensible to name them the IEE value)
I'm pretty sure this needs to go in a macro but i cant quite figure it.
Any help greatly appreciated
Splitting datasets is, in most cases, not needed and only causes extra work. You can extract a subset for a given analysis by using a WHERE condition, and you can use BY group processing to repeat a certain analysis fior groups in one step.
If, on the other hand, you need to export data to a different environment and, say, for different people, you can also use BY group processing to automatically create a separate file for each group.
So the question is: what do you really want to do with those subsets?
And what will you do with the end results of this?
While I think @Kurt_Bremser is mostly right when he states that splitting datasets is often a waste of time, sometimes you still have to. For instance if you are mailing or otherwise forwarding data to specific departments (or customers), and they are not supposed to get the data for the other departments.
A simple solution for creating multiple datasets goes something like this:
1. Create a solution that works for a single value of the parameter, e.g.
data want3007;
set have;
where IEE=3007;
run;
2. Make that into a macro, using the parameter:
%macro extract(IEE);
data want&IEE;
set have;
where IEE=&IEE;
run;
%mend;
3. Test the macro with a single, known parameter:
options mprint; /* lets you see the code generated in the log */
%extract(3007);
4. Use SQL to put all the macro calls into a single macro variable:
proc sql noprint;
select distinct cats('%extract(',IEE,')') into :doit separated by ';'
from have;
quit;
5. Execute that:
&doit;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.