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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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