Giter VIP home page Giter VIP logo

sas-generate-shift-table's Introduction

Generating a shift from baseline table

Intro

A shift from baseline table, also known as a shift table, is an n-by-n matrix in which the row and column positions correspond respectively to a category before and after a time point and/or treatment.

The code below generates a shift table by (1) tallying the count data using a multidimensional array and (2) calculating percentages for a shift table using matrix operations built into the SAS Function Compiler (PROC FCMP). (Unlike PROC IML, PROC FCMP is included in Base SAS.)

Illustration: Code for a 4x4 table

data scores;
	infile cards;
	input subjid $ visit score;
	cards;
1 0 4
1 1 3
2 0 1
2 1 3
3 0 2
3 1 2
4 0 2
4 1 2
;

proc sql ;
	select count(unique subjid) into :nsubj
		from scores
	;
quit;


data counts;
	set scores end=eof;
	by subjid;
	
	retain prepost1-prepost16 0 _pre _post;
	if first.subjid then call missing(_pre, _post);
	
	array prepost[4,4] prepost1-prepost16;
	if visit = 0 then _pre = score;
	else if visit = 1 then _post = score ;
	
	if last.subjid then do;
		prepost[_pre, _post] = sum(prepost[_pre, _post], 1);
	end;
	
	if eof then do;
		array post[4] post1-post4;
		do i = 1 to 4;
			pre = i;
			do j = 1 to 4;
				post[j] = prepost[i, j];						
			end;
			output;
		end;		

	end;
	keep post: ;
run;

	/******************/
	
proc fcmp ; 
	array counts[4, 4] / nosymbols;
	rc = read_array('counts', counts);
	
	array denom[4, 4] ;
	call fillmatrix(denom, 1/&nsubj);
	put denom=; 
	
	array pcts[4, 4];
	call elemmult(counts, denom, pcts);
		
	rc = write_array('counts', counts);
	rc = write_array('pcts', pcts);
run;

%*options cmplib=work.mylib;  /* not needed if only using pre-defined FCMP functions */


data shifttable;
	format pre post1-post4;
	set counts;
	set pcts;
	
	pre = _n_;
	
	length post1-post4 $20;
	
	array counts[4];
	array pcts[4];
	array post[4] $;
	do i = 1 to 4;
		if counts[i] > 0 and pcts[i] > 0 then post[i] = put(counts[i], 3.) || ' (' || substr(put(pcts[i], percent9.1), 3) || ')';
		else post[i] = put(counts[i], 3.);
	end;
	keep pre post: ;
run;

proc print data=&syslast (obs=20); 
run;

Posted 2017-08-21

SAS Tips

Portfolio

sas-generate-shift-table's People

Contributors

noorykim avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.