dynamic generation of data steps on basis of unique by-group permutations david rosenfeld city of...

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

Upload: kathlyn-ferguson

Post on 18-Jan-2018

214 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

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

Dynamic Generation of Data Stepson basis of Unique By-Group Permutations

David RosenfeldCity of Toronto

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

June 11, 2010TASS

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

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

June 11, 2010TASS

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 1Division_B 2Division_B 8Division_B 7Division_B 6Division_A 5;

The Data

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

June 11, 2010TASS

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

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

June 11, 2010TASS

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 26140http://support.sas.com/kb/26/140.html

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

June 11, 2010TASS

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

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

June 11, 2010TASS

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;

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

June 11, 2010TASS

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;

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

June 11, 2010TASS

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;

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

June 11, 2010TASS

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)

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

June 11, 2010TASS

Dynamic Generation of Data Steps on basis of Unique By-Group Permutations

The Output

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

June 11, 2010TASS

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

Conclusiona useful method to:

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

June 11, 2010TASS

Dynamic Generation of Data Steps on basis of Unique By-Group Permutations

Questions?