Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Generation of Data Steps on basis of Unique By-Group Permutations David Rosenfeld City of Toronto.

Similar presentations


Presentation on theme: "Dynamic Generation of Data Steps on basis of Unique By-Group Permutations David Rosenfeld City of Toronto."— Presentation transcript:

1 Dynamic Generation of Data Steps on basis of Unique By-Group Permutations David Rosenfeld City of Toronto

2 June 11, 2010 TASS Dynamic Generation of Data Steps on basis of Unique By-Group Permutations the data the problem a solution the macro the output the conclusion Overview

3 June 11, 2010 TASS Dynamic Generation of Data Steps on basis of Unique By-Group Permutations data tass_sample; input division $10. score; label score='Test Score'; cards; Division_A 1 Division_B 2 Division_B 8 Division_B 7 Division_B 6 Division_A 5 ; The Data

4 June 11, 2010 TASS Dynamic Generation of Data Steps on basis of Unique By-Group Permutations need to export separate worksheets for each division the task has to be repeated every week don’t know which divisions will be in the data file don’t want to hardcode (and have to change) the program every week The Problem

5 June 11, 2010 TASS Dynamic Generation of Data Steps on basis of Unique By-Group Permutations dynamically produce the desired datasets use subsetting criteria based on the values of a ‘by’ variable A Solution * Create a Macro to: derived from sample 26140 http://support.sas.com/kb/26/140.html

6 June 11, 2010 TASS Dynamic Generation of Data Steps on basis of Unique By-Group Permutations %macro groups(dsn,byvar,export_to); /* First sort the data in order of the */ /* by variable */ proc sort data=&dsn.; by &byvar.; run; The Macro

7 June 11, 2010 TASS Dynamic Generation of Data Steps on basis of Unique By-Group Permutations %macro groups(dsn,byvar,export_to); /* First sort the data in order of the */ /* by variable */ proc sort data=&dsn.; by &byvar.; run; The Macro /* Then, create a macro variable, VARn, for */ /* each BY-Group and a counter of the number */ /* of new macro variables created. */ data _null_; set &dsn. end=eof; by &byvar.; if first.&byvar. then do; flag+1; call symput('var'||put(flag,8. -L),&byvar.); end; if eof then call symput('tot',put(flag,8. -L)); run;

8 June 11, 2010 TASS Dynamic Generation of Data Steps on basis of Unique By-Group Permutations %macro groups(dsn,byvar,export_to); /* First sort the data in order of the */ /* by variable */ proc sort data=&dsn.; by &byvar.; run; The Macro /* Then, create a macro variable, VARn, for */ /* each BY-Group and a counter of the number */ /* of new macro variables created. */ data _null_; set &dsn. end=eof; by division; if first.division then do; flag+1; call symput('var'||put(flag,8. -L),division); end; if eof then call symput('tot',put(flag,8. -L)); run; /* Loop through all the macro variables and */ /* create a separate file for each by variable */ data %do i=1 %to &tot; &&var&i %end;; set &dsn; %do i=1 %to &tot; if &byvar="&&var&i" then output &&var&i; %end; run;

9 June 11, 2010 TASS Dynamic Generation of Data Steps on basis of Unique By-Group Permutations %macro groups(dsn,byvar,export_to); /* First sort the data in order of the */ /* by variable */ proc sort data=&dsn.; by &byvar.; run; The Macro /* Then, create a macro variable, VARn, for */ /* each BY-Group and a counter of the number */ /* of new macro variables created. */ data _null_; set &dsn. end=eof; by division; if first.division then do; flag+1; call symput('var'||put(flag,8. -L),division); end; if eof then call symput('tot',put(flag,8. -L)); run; /* Loop through all the macro variables and */ /* create a separate file for each by variable */ data %do i=1 %to &tot; &&var&i %end;; set &dsn; %do i=1 %to &tot; if &byvar="&&var&i" then output &&var&i; %end; run; /* Loop through all the macro variables and */ /* export a sheet for each by variable */ %do j=1 %to &tot; PROC EXPORT DATA= WORK.&&var&j OUTFILE= "&export_to." DBMS=EXCEL REPLACE; /* dbdsopts='dblabel=yes'; */ SHEET="&&var&j"; RUN; %end; %mend groups;

10 June 11, 2010 TASS Dynamic Generation of Data Steps on basis of Unique By-Group Permutations %macro groups(dsn,byvar,export_to); /* First sort the data in order of the */ /* by variable */ proc sort data=&dsn.; by &byvar.; run; The Macro /* Then, create a macro variable, VARn, for */ /* each BY-Group and a counter of the number */ /* of new macro variables created. */ data _null_; set &dsn. end=eof; by division; if first.division then do; flag+1; call symput('var'||put(flag,8. -L),division); end; if eof then call symput('tot',put(flag,8. -L)); run; /* Loop through all the macro variables and */ /* create a separate file for each by variable */ data %do i=1 %to &tot; &&var&i %end;; set &dsn; %do i=1 %to &tot; if &byvar="&&var&i" then output &&var&i; %end; run; /* Loop through all the macro variables and */ /* export a sheet for each by variable */ %do j=1 %to &tot; PROC EXPORT DATA= WORK.&&var&j OUTFILE= "&export_to." DBMS=EXCEL REPLACE; /* dbdsopts='dblabel=yes'; */ SHEET="&&var&j"; RUN; %end; %mend groups; /* Run the macro */ %groups(work.tass_sample, division, c:\data_driven_demo.xls)

11 June 11, 2010 TASS Dynamic Generation of Data Steps on basis of Unique By-Group Permutations The Output

12 June 11, 2010 TASS Dynamic Generation of Data Steps on basis of Unique By-Group Permutations remove annoying drudgery avoid having to continually check data eliminate tedious and inefficient hard coding promote automated and easily maintained sparse code have code that is flexible and reacts to data transparently Conclusion a useful method to:

13 June 11, 2010 TASS Dynamic Generation of Data Steps on basis of Unique By-Group Permutations Questions?


Download ppt "Dynamic Generation of Data Steps on basis of Unique By-Group Permutations David Rosenfeld City of Toronto."

Similar presentations


Ads by Google