Combining Lags and Arrays [and a little macro] Lynn Lethbridge SHRUG OCT 28, 2011.

Slides:



Advertisements
Similar presentations
A guide to the unknown…  A dataset is longitudinal if it tracks the same type of information on the same subjects at multiple points in time or space.
Advertisements

Outline Proc Report Tricks Kelley Weston. Outline Examples 1.Text that spans columnsText that spans columns 2.Patient-level detail in the titlesPatient-level.
Line Efficiency     Percentage Month Today’s Date
Unit Number Oct 2011 Nov 2011 Dec 2011 Jan 2012 Feb 2012 Mar 2012 Apr 2012 May 2012 Jun 2012 Jul 2012 Aug 2012 Sep (3/4 Unit) 7 8 Units.
Phase Shifts of Sinusoidal Graphs. We will graph sine functions of the following form: The amplitude A =  A  The period T = 22  The phase shift comes.
Into to SAS ®. 2 List the components of a SAS program. Open an existing SAS program and run it. Objectives.
Welcome to SAS…Session..!. What is SAS..! A Complete programming language with report formatting with statistical and mathematical capabilities.
Introduction to SAS BIO 226 – Spring Outline Windows and common rules Getting the data –The PRINT and CONTENT Procedures Manipulating the data.
Key Data Management Tasks in Stata
SAS Efficiency Techniques and Methods By Kelley Weston Sr. Statistical Programmer Quintiles.
The General. What happens to the graph of a sine function if we put a coefficient on the x. y = sin 2x y = sin x It makes the graph "cycle" twice as fast.
Advanced Work with Embedded and Summative Assessment Dr. Steve Broskoske Misericordia University EDU 533 Computer-based Education.
End of unit assessment Challenge 1 & 2. Course summary So far in this course you have learnt about and used: Syntax Output to screen (PRINT) Variables.
Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Time Series Data Processes by Tai Yu April 15, 2013.
Computing with SAS Software A SAS program consists of SAS statements. 1. The DATA step consists of SAS statements that define your data and create a SAS.
Jan 2016 Solar Lunar Data.

ITI Portfolio Plan Aug Sep Oct Nov Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Current Date Visibility of ITI Projects ITI Projects.
Analyzing patterns in the phenomena
Q1 Jan Feb Mar ENTER TEXT HERE Notes

Project timeline # 3 Step # 3 is about x, y and z # 2
Average Monthly Temperature and Rainfall
SAS Essentials How SAS Thinks
SET statement in DATA step
مفاهیم بهره وري.


2017 Jan Sun Mon Tue Wed Thu Fri Sat
Timeline PowerPoint Template


Mississippi River at Clinton, Iowa.
This Presentation Pack is brought to you by
Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Free PPT Diagrams : ALLPPT.com


Step 3 Step 2 Step 1 Put your text here Put your text here
MONTH CYCLE BEGINS CYCLE ENDS DUE TO FINANCE JUL /2/2015
Jan Sun Mon Tue Wed Thu Fri Sat
Let’s Talk About Variable Attributes

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ITEM 1 ITEM 2 ITEM 3
Electricity Cost and Use – FY 2016 and FY 2017
QUIZ.


Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Free PPT Diagrams : ALLPPT.com


Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Project timeline # 3 Step # 3 is about x, y and z # 2
TIMELINE NAME OF PROJECT Today 2016 Jan Feb Mar Apr May Jun
Charlie Haffey Norwood Public Schools

Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Presentation transcript:

Combining Lags and Arrays [and a little macro] Lynn Lethbridge SHRUG OCT 28, 2011

Lag Function  A tool to manipulate data across observations  Can lag back as far as you like (default is a lag of 1)  Can be frustrating! Don’t put a lag in a conditional statement; create a variable with the lag value first and use the variable in the conditional statement

Example 1a Obs x sex z 1 1 M. 2 1 M F. 4 5 M F. 6 8 F. 7 8 F. 8 8 F. 9 9 F M 9 data work.temp; input x sex $; lx=lag(x); if sex='M' then z=lx; drop lx; cards; 1 M 3 F 5 M 5 F 8 F 9 F 10 M ; run;

Example 1b data work.temp; input x sex $; if sex='M' then z=lag(x); cards; 1 M 3 F 5 M 5 F 8 F 9 F 10 M ; run; Obs x sex z 1 1 M. 2 1 M F. 4 5 M F. 6 8 F. 7 8 F. 8 8 F. 9 9 F M 5

Arrays  Allows you to assign a single name to multiple variables  Can use an array to manipulate data across variables without having to write them out multiple times  Can create new variables in arrays

data work.temp; input id sex $ a b c; array oldvars {*} a b c; array newvars {*} var1-var3; do i=1 to dim(oldvars); if sex='f' then newvars{i}=oldvars{i}; end; drop i; cards; 1 m m m f m f f m ; run;

Obs id sex a b c var1 var2 var3 1 1 m m m f m f f m

Lags and Arrays  Lags allow you to work across observations  Arrays allow you to work across variables  Combining the two lets you work efficiently with your data as a matrix

Combined Example  Suppose you have multiple observations per ID  Ordered first by ID and then by date  You want to know if a code appears twice in a row for any individual ID across multiple variables

data work.temp; input id date a $ b $ c $; cards; a1 b2 g a4 a1 v s3 f5 g a1 a1 g h6 i8 t b2 f4 f d4 f4 s c1 g4 d f4 d4 s d1 s2 w a1 s2 d a1 a2 a f4 f3 f d1 a1 a1 ; run; Data

Lags and Arrays data work.temp2; set work.temp; lagid=lag(id); array vars {*} a b c; array newvars {*} $ na nb nc; do i=1 to 3; lagvars=lag(vars{i}); if id=lagid and lagvars=vars{i} then newvars{i}=vars{i}; end; drop lagid i lagvars; run;

Printed Output Obs id date a b c na nb nc JUN95 a1 b2 g AUG95 a4 a1 v SEP97 s3 f5 g JUN90 a1 a1 g JUN93 h6 i8 t JUL96 b2 f4 f JUL99 d4 f4 s3 f MAY87 c1 g4 d JUN90 f4 d4 s FEB87 d1 s2 w MAY87 a1 s2 d1 s NOV96 a1 a2 a1 a NOV99 f4 f3 f OCT03 d1 a1 a1

Higher Order Lag data work.temp; input id a ; la3=lag3(a); cards; ; run; Obs a la

Higher Order Lag with a Macro %let num=3; data work.temp; input id a ; la&num=lag&num(a); cards; ; run; Obs a la

Thank you for your attention!