I am trying to implement http://support.sas.com/documentation/cdl/en/lestmtsref/68024/HTML/default/viewer.htm#p08do6szetrxe2n... example 2 with characters as so, how can this be done?
data old;
input start end $;
datalines;
1 a
1 b
1 c
1 d
1 e
1 f
1 g
2 a
2 b
3 a
3 b
3 c
3 d
3 e
;
data new(drop=i count);
set old;
by start;
/* Create and assign values to three new variables. Use ENDLAG1- */
/* ENDLAG3 to store lagged values of END, from the most recent to the */
/* third preceding value. */
array x(*) endlag1-endlag3;
endlag1=lag1(end);
endlag2=lag2(end);
endlag3=lag3(end);
/* Reset COUNT at the start of each new BY-Group */
if first.start then count=1;
/* On each iteration, set to missing array elements */
/* that have not yet received a lagged value for the */
/* current BY-Group. Increase count by 1. */
do i=count to dim(x);
x(i)=.;
end;
count + 1;
run;
If I understand that you are looking to do the same processing with a character array
data new(drop=i count);
set old;
by start;
/* since your first reference to the variables Endlag1 to EndLag3
is in the array statement you need to indicate that they are
character and HOW long they are, $ for character 1 for length
if you used a LENGTH Endlag1-Endlag3 $ 1 prior to the array
then the example definition would have worked*/
array x(3) $1 endlag1-endlag3;
endlag1=lag1(end);
endlag2=lag2(end);
endlag3=lag3(end);
if first.start then count=1;
/* On each iteration, set to missing array elements */
/* that have not yet received a lagged value for the */
/* current BY-Group. Increase count by 1. */
do i=count to dim(x);
call missing(x(i)); /*=. doesn't work for character to set missing.
Call missing works with both numeric and character*/
end;
count + 1;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.