/******************* URBAN INSTITUTE MACRO LIBRARY *********************
: Super_transpose
Macro: Autocall macro that performs transpose on a series of vars in a
Description
data set and combines results together into a single file.
: Open code
Use
: Peter Tatian
Author
***********************************************************************/
Super_transpose(
%macro data= , /** Input data set **/
out= , /** Output data set **/
var= , /** List of variables to transpose **/
id= , /** Input data set var. to use for transposing **/
by= , /** List of BY variables (opt.) **/
mprint=N /** Print macro code to LOG (Y/N) **/
);
/*************************** USAGE NOTES *****************************
:
SAMPLE CALLSuper_transpose(
%data=Test,
out=Test_res,
id=idvar,
by=byvar,
var=var1 var2
)
transposes data set Test variables var1 and var2 using idvar
to define variable names and byvar to define observations.
result is saved to data set Test_res.*********************************************************************/
/*************************** UPDATE NOTES ****************************
06/24/06 Peter A. Tatian
02/23/11 PAT Added declaration for local macro vars.
06/02/21 PAT Fixed %if issues with empty macro vars.
*********************************************************************/
***** ***** ***** MACRO SET UP ***** ***** *****;
%
%local i tvar files j val;
***** ***** ***** ERROR CHECKS ***** ***** *****;
%
***** ***** ***** MACRO BODY ***** ***** *****;
%
Note_mput( macro=Super_transpose, msg=Macro starting. )
%
** Save current MPRINT setting and reset based on MPRINT= parameter **;
%
Push_option( mprint )
%
%if %mparam_is_yes( &mprint ) %then %do;
options mprint;
%end;%else %do;
options nomprint;
%end;
** Check input parameters **;
%
%if %length( &var ) = 0 %then %do;
Err_mput( macro=Super_transpose, msg=Parameter VAR= cannot be blank. )
%
%goto exit;
%end;
** Begin main macro **;
%
** Get list of ID values for label creation **;
%
=&data nway;
proc summary data&id;
class =_st_idvals (drop=_type_ _freq_ compress=no);
output out
data _null_;$ 5000;
length idvals "";
retain idvals =eof;
set _st_idvals end= trim( idvals ) || " " || left( &id );
idvals if eof then call symput( "idvals", idvals );
run;
=&idvals;
%put idvals
** Create individual transposed files for each variable **;
%
= 1;
%let i %let tvar = %scan( &var, &i );
= ;
%let files
%do %until ( &tvar = );
** Get variable label **;
data _null_;&data (obs=1 keep=&tvar);
set symput( "&tvar._lbl", vlabel( &tvar ) );
call
run;
** Transpose &tvar **;
=&data
proc transpose data=_st_&tvar (keep=&by &tvar._: )
out=&tvar._;
prefix&tvar;
var &id;
id %if %length( &by ) ~= 0 %then %do;
&by;
by
%end;
run;
= &files _st_&tvar;
%let files
%let i = %eval( &i + 1 );
%let tvar = %scan( &var, &i );
%end;
** Combine transposed files **;
&out;
data
&files;
merge %if %length( &by ) ~= 0 %then %do;
&by;
by
%end;
** Label variables **;
= 1;
%let i %let tvar = %scan( &var, &i );
%do %until ( &tvar = );
%**%put tvar_lbl=&tvar_lbl;
= 1;
%let j %let val = %scan( &idvals, &j );
label
%do %until ( &val = );
&tvar._&val = "&&&tvar._lbl, &val"
%let j = %eval( &j + 1 );
%let val = %scan( &idvals, &j );
%end;
;
%let i = %eval( &i + 1 );
%let tvar = %scan( &var, &i );
%end;
run;
** Exit macro **;
%
:
%exit
***** ***** ***** CLEAN UP ***** ***** *****;
%
** Restore system options **;
%
Pop_option( mprint )
%
Note_mput( macro=Super_transpose, msg=Macro exiting. )
%
%mend Super_transpose;
/************************ UNCOMMENT TO TEST ***************************
options nocenter;** Locations of SAS autocall macro libraries **;
"K:\Metro\PTatian\UISUG\Uiautos";
filename uiautos =(uiautos sasautos);
options sasautos
data Test;$ idvar var1 var2 $;
input byvar
label = 'By variable'
byvar = 'ID variable'
idvar = 'Variable #1'
var1 = 'Variable #2';
var2
datalines;1 11 12
A 2 21 22
A 3 31 32
A 4 41 42
A 1 11 12
B 2 21 22
B 4 41 42
B 1 11 12
C 2 21 22
C
;
run;File_info( data=Test, stats=, printobs=10 );
%Super_transpose(
%data=Test,
out=Test_res,
id=idvar,
by=byvar,
var=var1 var2,
mprint=y
)File_info( data=Test_res, stats=, printobs=10 )
%/**********************************************************************/
Transpose Variables
Purpose: Autocall macro that performs transpose on a series of vars in a data set and combines results together into a single file.