BookmarkSubscribeRSS Feed
dwsmith
Obsidian | Level 7

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;
1 REPLY 1
ballardw
Super User

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 1030 views
  • 0 likes
  • 2 in conversation