Concatenate All Values Into Constant
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | *(Q) I have a string variable var1 that may contain entries x_1 to x_n interspersed with missing values (n is unknown in advance). I would like to create a new constant string variable newvar which is simply a concatenation of all the non sysmissing values in var1. *(A) By rlevesque@videotron.ca 2001/10/09. DATA LIST LIST /var1(A8). BEGIN DATA . x1 . . x2 x3 . x4 . . END DATA. LIST. COMPUTE dummy=1. SAVE OUTFILE='c:\\temp\\mydata.sav'. SELECT IF LTRIM(RTRIM(var1))<>".". COMPUTE idx=$CASENUM. STRING newvar(A255). DO IF idx=1. COMPUTE newvar=var1. ELSE. COMPUTE newvar=CONCAT(RTRIM(LAG(newvar))," ",var1). END IF. EXECUTE. COMPUTE dummy=1. ADD FILES FILE=* /BY=dummy /LAST=last. EXECUTE. SELECT IF last=1. SAVE OUTFILE='c:\\temp\\mystring.sav'. GET FILE='c:\\temp\\mydata.sav'. MATCH FILES FILE=* /TABLE='c:\\temp\\mystring.sav' /BY dummy. * Clean up. MATCH FILES FILE=* /DROP=dummy idx. EXECUTE. |
Related pages
...