1
+ /************************************************************************************************
2
+ SPLIT TABLE BY COLUMN
3
+ A macro program that splits a table by the unique values of a specified column.
4
+ Keywords: Macro
5
+ SAS Versions: SAS 9, SAS Viya
6
+ Documentation: https://go.documentation.sas.com/doc/en/pgmsascdc/default/mcrolref/titlepage.htm
7
+ 1. Create a macro function called SplitTableByColumn that takes two parameters
8
+ 2. Use PROC SQL to extract all unique values from a specific column in a dataset and store them
9
+ in a macro variable.
10
+ 3. Use the macro variable created in step 1 to split the input table into multiple datasets.
11
+ 4. End the macro definition.
12
+ 5. Example usage of the macro.
13
+ Note: This macro does not work if the unique values in the specified column
14
+ contain spaces or special characters that are not valid in SAS dataset names.
15
+ ************************************************************************************************/
16
+
17
+ /************************************************************************************************
18
+ 1. Create a macro function called SplitTableByColumn that takes two parameters:
19
+ a. input_table: the name of the input dataset to be split.
20
+ b. split_column: the name of the column whose unique values will determine how the table is split.
21
+ The macro will create separate datasets for each unique value in the specified column.
22
+ ************************************************************************************************/
23
+ %macro SplitTableByColumn (input_table, split_column);
24
+ /************************************************************************************************
25
+ 2. use PROC SQL to extract all unique values from a specific column in a dataset and store them
26
+ in a macro variable.
27
+ a. proc sql starts a SQL procedure in SAS.
28
+ b. The noprint option tells SAS not to display the usual output in the results window, as this
29
+ PROC SQL is only to create macro variables.
30
+ c. select distinct &split_column selects all unique (distinct) values from &split_column.
31
+ d. into :unique_values separated by ' ' stores all the resulting unique values as a list in one
32
+ macro variable called unique_values. The separated by ' ' part means that the values will be
33
+ joined together in the macro variable, separated by spaces.
34
+ e. from &input_table; specifies the source table, using the macro variable &input_table to
35
+ refer to the dataset.
36
+ f. quit; ends the PROC SQL step.
37
+ ************************************************************************************************/
38
+ proc sql noprint;
39
+ select distinct &split_column into :unique_values separated by ' '
40
+ from &input_table;
41
+ quit ;
42
+
43
+ /************************************************************************************************
44
+ 3. Use the macro variable created in step 1 to split the input table into multiple datasets.
45
+ a. A data step is used to create separate datasets for each unique value.
46
+ b. The set statement reads from the input table specified by the macro variable input_table.
47
+ c. A loop iterates over the list of unique values stored in the macro variable unique_values.
48
+ It does this by using the %scan function to extract the first to the last unique value,
49
+ represented by the macro variable sqlobs, which is automatically updated by PROC SQL. It
50
+ assigns the current unique value to a local scope macro variable called current_value.
51
+ d. The if statement checks if the current row's value in the macro variable split_column
52
+ matches the current unique value, and if so, outputs that row to a new dataset named after
53
+ the unique value.
54
+ ************************************************************************************************/
55
+ data &unique_values ;
56
+ set &input_table ;
57
+ %do i =1 %to &sqlobs ;
58
+ %local current_value ;
59
+ %let current_value =%scan (&unique_values , &i );
60
+ if &split_column = "¤t_value" then output ¤t_value ;
61
+ %end ;
62
+ run ;
63
+ /************************************************************************************************
64
+ 4. End the macro definition.
65
+ a. The %mend statement marks the end of the macro definition.
66
+ ************************************************************************************************/
67
+ %mend SplitTableByColumn ;
68
+
69
+ /************************************************************************************************
70
+ 5. Example usage of the macro.
71
+ a. Call the macro with the input table SASHELP.CARS and split by the Origin
72
+ *************************************************************************************************/
73
+ %SplitTableByColumn(input_table=SASHELP .CARS, split_column=Origin)
0 commit comments