let’s be friends with arrays presentation.pdf · arrays. vibhavari inamdar. any views or opinions...
TRANSCRIPT
Let’s be friends with Arrays
Vibhavari Inamdar
Any views or opinions presented in this presentation are solely those of the author and do not necessarily represent those of the company.
www.cytel.com ©2012 Cytel 2
Basic concepts of Array:
What is array Syntax of array Temporary Array Multidimensional arrays
Illustrating the usage of arrays in different scenarios from clinical programming
Agenda
www.cytel.com ©2012 Cytel 3
A means for repetitively processing variables .
A convenient way of grouping variables.
Wonderful tool which can be used to refer repetitive values.
What is Array?
www.cytel.com ©2012 Cytel 4
Syntax of an array statement
array array-name { n } [ $ ] [ length ] variable-names
www.cytel.com ©2012 Cytel 5
Syntax of an array statement
array
array-name{ n } [ $ ] [ length ] variable-names
A valid SAS name
www.cytel.com ©2012 Cytel 6
Syntax of an array statement
array array-name
{ n }
[ $ ] [ length ] variable-names
Dimension of the array
www.cytel.com ©2012 Cytel 7
Syntax of an array statement
array array-name { n }
[ $ ]
[ length ] variable-names
Character element
www.cytel.com ©2012 Cytel 8
Syntax of an array statement
array array-name { n } [ $ ]
[ length ]
variable-names
Assign lengths to character variables
www.cytel.com ©2012 Cytel 9
Syntax of an array statement
array array-name { n } [ $ ] [ length ]
variable-names
Names of the variables :Array elements
www.cytel.com ©2012 Cytel 10
Temporary Array :
•Arrays which are required only for calculations and not required in the output dataset.
•The elements are defined by the keyword _TEMPORARY_.
Temporary Array & Multidimensional Arrays
Multidimensional Arrays :•Used when the processing of data depends on more than one factor.
•Created in two or more dimensions.
www.cytel.com ©2012 Cytel 11
Illustrating the usage of arrays in different scenarios from in clinical programming
www.cytel.com ©2012 Cytel 12
Example 1:Creating Race variableData:
www.cytel.com ©2012 Cytel 13
Assign length for new variable “Race”
Read SAS dataset
Define Array
Do Loop
Example 1:Creating Race variable
www.cytel.com ©2012 Cytel 14
Assign length for new variable “Race”
Read SAS dataset
Define Array
Do Loop
Data dm_race(keep=usubjid Race);
length Race $56;
Example 1:Creating Race variable
www.cytel.com ©2012 Cytel 15
Assign length for new variable “Race”
Read SAS dataset
Define Array
Do Loop
data dm3(keep=usubjid Race);
length Race $56;
set dm2;
Example 1:Creating Race variable
www.cytel.com ©2012 Cytel 16
Assign length for new variable “Race”
Read SAS dataset
Define Array
Do Loop
array RACE_label1(*)$ BLACK ASIAN CAUSASIAN;
Example 1:Creating Race variable
www.cytel.com ©2012 Cytel 17
Assign length for new variable “Race”
Read SAS dataset
Define Array
Do Loop
do i =1 to dim(RACE_label1);
if RACE_label1(i) ne '' then
Call label(RACE_label1(i),Race);
end;
CALL LABEL :
Extracts label of array element and puts in variable “Race”
Example 1:Creating Race variable
Example 1:Creating Race variable
Assign length for newvariable “Race”
Define array with Racevariables as array elements
Do Loop
Use Call_Label and assign the label of array element in variable “Race”
Recap of example 1:
www.cytel.com ©2012 Cytel 18
www.cytel.com ©2012 Cytel 19
Output:
Input Data:
Example 1:Creating Race variable
www.cytel.com ©2012 Cytel 20
Example2:Calculating percentages in the table
Data:
www.cytel.com ©2012 Cytel 21
Table:
Example2:Calculating percentages in the table
www.cytel.com ©2012 Cytel 22
Number of subjects in each treatment:
Frequencies:
Example2:Calculating percentages in the table
Use proc sql and create macro variables “n1”, “n2”,”n3” with number of subjects under each treatment.
www.cytel.com ©2012 Cytel 23
Read SAS dataset
Define Array
Do Loop
data final2
(keep = gender _1D0 _2D0 _0D3 per1
per2 per3 );
set gen;
Example2:Calculating percentages in the table
www.cytel.com ©2012 Cytel 24
Read SAS dataset
Define Array
Do Loop
array no{3}_temporary_ (&n1.,&n2. , &n3.);
array freq{3} _1D0 _2D0 _0D3 ;
array percent{3}percent1 percent2 percent3;
array per{3}$ 18 per1 per2 per3;
Creating temporary array for number of subjects in each treatment
Example2:Calculating percentages in the table
www.cytel.com ©2012 Cytel 25
Read SAS dataset
Define Array
Do Loop
array no{3}_temporary_ (&n1.,&n2. , &n3.);
array freq{3} _1D0 _2D0 _0D3 ;
array percent{3}percent1 percent2 percent3;
array per{3}$ 18 per1 per2 per3;
Array for frequencies
Example2:Calculating percentages in the table
www.cytel.com ©2012 Cytel 26
Read SAS dataset
Define Array
Do Loop
array no{3}_temporary_ (&n1.,&n2. , &n3.);
array freq{3} _1D0 _2D0 _0D3 ;
array percent{3}percent1 percent2 percent3;
array per{3}$ 18 per1 per2 per3;
Array for percentages
Example2:Calculating percentages in the table
www.cytel.com ©2012 Cytel 27
Read SAS dataset
Define Array
Do Loop
array no{3}_temporary_ (&n1.,&n2. , &n3.);
array freq{3} _1D0 _2D0 _0D3 ;
array percent{3}percent1 percent2 percent3;
array per{3}$ 18 per1 per2 per3;
Array for concatenation of frequencies and percentages
Example2:Calculating percentages in the table
www.cytel.com ©2012 Cytel 28
Read SAS dataset
Define Array
Do Loop
do i=1 to 3;
if freq{i} eq . then freq{i} =0;
percent{i}=(freq{i}/no{i})*100;
per{i}=
compress(put(freq{i},5.0))||''||'('||
compress(put(Percent{i},5.2) )|| ')';
end;
Replacing blanks with Zero
Example2:Calculating percentages in the table
www.cytel.com ©2012 Cytel 29
Read SAS dataset
Define Array
Do Loop
do i=1 to 3;
if freq{i} eq . then freq{i} =0;
percent{i}=(freq{i}/no{i})*100;
per{i}=
compress(put(freq{i},5.0))||''||'('||
compress(put(Percent{i},5.2) )|| ')';
end;
Calculating percentages
Example2:Calculating percentages in the table
www.cytel.com ©2012 Cytel 30
Read SAS dataset
Define Array
Do Loop
do i=1 to 3;
if freq{i} eq . then freq{i} =0;
percent{i}=(freq{i}/no{i})*100;
per{i}=
compress(put(freq{i},5.0))||''||'('||
compress(put(Percent{i},5.2) )|| ')';
end;
Concatenating the frequencies andpercentages
Example2:Calculating percentages in the table
Recap of example 2:
www.cytel.com
Example2:Calculating percentages in the table
Define array
Replacing blanks with Zero
Calculating the percentages
Concatenating the frequencies and percentages
©2012 Cytel 31
www.cytel.com ©2012 Cytel 32
Example3:Imputing dates by adding 6 months
Data: Missing date
www.cytel.com ©2012 Cytel 33
Transposing the data:
Example3:Imputing dates by adding 6 months
www.cytel.com ©2012 Cytel 34
Read SAS dataset
Define Array
Do Loop
data ex3;set _ex2;by usubjid;
Example3:Imputing dates by adding 6 months
www.cytel.com ©2012 Cytel 35
Read SAS dataset
Define Array
Do Loop
array st(7) ms: ;
Example3:Imputing dates by adding 6 months
www.cytel.com ©2012 Cytel 36
Read SAS dataset
Define Array
Do Loop
do i=1 to 7;if i gt 1 and st(i)=. and st(i-1) ne .
then do;
If the date is missing but theprevious date is not missing
Example3:Imputing dates by adding 6 months
www.cytel.com ©2012 Cytel 37
Read SAS dataset
Define Array
Do Loop
st(i)=intnx('month',st(i-1),6);
dd=day(st(i-1));
mm=month(st(i));
yy=year(st(i));
intnx function returns date value
incremented by a specified number of intervals .
Example3:Imputing dates by adding 6 months
www.cytel.com ©2012 Cytel 38
Read SAS dataset
Define Array
Do Loop
if dd lt 29 then st(i)=mdy(mm,dd,yy);
else if dd ge 29 and mm=2 then do;
if mod(yy,4)=0 then st(i)=mdy(mm,29,yy);
else st(i)=mdy(mm,28,yy);
end;
While calculating new date,consider if the month is Feb based on date
less than or equal to 29
Example3:Imputing dates by adding 6 months
www.cytel.com ©2012 Cytel 39
Read SAS dataset
Define Array
Do Loop
if dd lt 29 then st(i)=mdy(mm,dd,yy);
else if dd ge 29 and mm=2 then do;
if mod(yy,4)=0 then st(i)=mdy(mm,29,yy);
else st(i)=mdy(mm,28,yy);
end;
Consider the Leap year to assign date 29.
Example3:Imputing dates by adding 6 months
www.cytel.com ©2012 Cytel 40
Read SAS dataset
Define Array
Do Loop
else if dd ge 29 and mm ne 2 then do;
if dd in (29,30) thenst(i)=mdy(mm,dd,yy);
else if dd=31 then do;
if mm in (1,3,5,7,8,10,12) thenst(i)=mdy(mm,dd,yy);
else if mm in (4,6,9,11) thenst(i)=mdy(mm,30,yy);
If month is not Feb andif date is 31 then check the month of new date.
Example3:Imputing dates by adding 6 months
Recap of example 3:
www.cytel.com
Define array
If date is missingand the previous date is not missing
Intnx function and get new date
Checks on new date
Example3:Imputing dates by adding 6 months
©2012 Cytel 41
www.cytel.com ©2012 Cytel 42
Output:
Imputed Dates!!
Example3:Imputing dates by adding 6 months
www.cytel.com ©2012 Cytel 43
Input Data:
Example 4:To find the consecutive same result
www.cytel.com ©2012 Cytel 44
Example 4:To find the consecutive same result
Transposing the data:
www.cytel.com ©2012 Cytel 45
Task:
If the baseline is “positive”
If result is “Negative” for consecutive visits.
At which visit the result turn tobe Negative
Example 4:To find the consecutive same result
www.cytel.com ©2012 Cytel 46
Read dataset and check if Baseline is positive
Define Array
Do Loop
data conc;
set ts;
if ag1='Positive' then do;
flag1=1;
end;
.Read the SAS
dataset
Example 4:To find the consecutive same result
www.cytel.com ©2012 Cytel 47
Define Array
Do Loop
data conc;
set ts;
if ag1='Positive' then do;
flag1=1;
end;
Read dataset and check if Baseline is positive
Example 4:To find the consecutive same result
If Baseline is positivethen flag1=1
www.cytel.com ©2012 Cytel 48
Define Array
Do Loop
array b(2:5) $ ag2-ag5;
Read dataset and check if Baseline is positive
Example 4:To find the consecutive same result
www.cytel.com ©2012 Cytel 49
Define Array
Do Loop
do i=2 to 4;
if b(i)="Negative" and b(i+1)="Negative"
then do;
visit=i ;
flag2=1;
i=i+4;
end;
end;
If the consecutive visits have result “Negative”
Read dataset and check if Baseline is positive
Example 4:To find the consecutive same result
www.cytel.com ©2012 Cytel 50
Define Array
Do Loop
do i=2 to 4;
if b(i)="Negative" and b(i+1)="Negative"
then do;
visit=i ;
flag2=1;
i=i+4;
end;
end;
Visit at which the result is consecutively
Negative
Read dataset and check if Baseline is positive
Example 4:To find the consecutive same result
www.cytel.com ©2012 Cytel 51
Define Array
Do Loop
do i=2 to 4;
if b(i)="Negative" and b(i+1)="Negative"
then do;
visit=i ;
flag2=1;
i=i+4;
end;
end;
Read dataset and check if Baseline is positive
Example 4:To find the consecutive same result
If consecutive visits are “negative” then flag2=1
www.cytel.com ©2012 Cytel 52
Define Array
Do Loop
do i=2 to 4;
if b(i)="Negative" and b(i+1)="Negative"
then do;
visit=i ;
flag2=1;
i=i+4;
end;
end;
End the loop if consecutive visits have Negative value.
Read dataset and check if Baseline is positive
Example 4:To find the consecutive same result
www.cytel.com ©2012 Cytel 53
data conc;
set ts;
if ag1='Positive' then do;
flag1=1;
end;
array b(2:5) $ ag2-ag5;
do i=2 to 4;
if b(i)="Negative" and b(i+1)="Negative" then do;
visit=i ;
flag2=1;
i=i+4;
end;
end;
if flag1=1 and flag2>1 then censor=0;
else censor=1;
If baseline is positive and consecutive visits are Negative.
Example 4:To find the consecutive same result
Recap of example 4:
www.cytel.com
Check if baseline is “positive”
Define array
Check if consecutive visits have result
“Negative”
visit where the result turn to be Negative and censor variable
Example 4:To find the consecutive same result
©2012 Cytel54
www.cytel.com ©2012 Cytel 55
Some more examples where ARRAY would be of great help
• Calculation of AE Phase
Different periods of the study
Start date and end date of different date
Define array
Calculate AE phase
• Deriving Cycle Variable
Dosing period and Rest Period of each cycle.
Start date of each cycle.
Define array
To calculate End date of cycle.
• And Many more….
www.cytel.com ©2012 Cytel 56
Limitations
1. Variables should be either all numeric or character.
2. The dimensions must be supplied at compile time by either hardcoding or macro variables.
3. Array takes longer duration during compiling.
Advantages
1. Array can be used to perform same operation over a set of variables more efficiently.
2. Array can be used to create new variables.
3. Array helps to reduce code length.
4. Program becomes more efficient and saves your time and energy.
www.cytel.com ©2012 Cytel 57
www.cytel.com ©2012 Cytel 58
Conclusion
•Arrays give us lot of flexibility to manipulate data in different
ways.
•Hope we have raised the toast of friendship towards Arrays and
will consider arrays as convenient approach of programming!!
www.cytel.com ©2012 Cytel 59
Questions?
www.cytel.com ©2012 Cytel 60
Thank [email protected]