DATA Step, Macro, Functions and more

SAS example in documentation how to use when character?

Reply
Contributor
Posts: 40

SAS example in documentation how to use when character?

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;
Super User
Posts: 11,343

Re: SAS example in documentation how to use when character?

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;
Ask a Question
Discussion stats
  • 1 reply
  • 167 views
  • 0 likes
  • 2 in conversation