Date: Wed, 28 May 2008 07:04:01 -0700
Reply-To: "Richard A. DeVenezia" <rdevenezia@WILDBLUE.NET>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Richard A. DeVenezia" <rdevenezia@WILDBLUE.NET>
Organization: http://groups.google.com
Subject: Re: Many variables to one variable
Content-Type: text/plain; charset=windows-1252
On May 28, 7:35 am, len...@SOCSCI.AAU.DK (Lene Blenstrup) wrote:
> Hello all...
>
> I have 23 variables that I would like to combine into one variable. Each
> variable can take the values 1, 2 and 3.
> Instead of 23 variables describing the sequence, I would like just one.
> I have the following variables (named 1981-2003) and would like the new
> variable “WANT”:
>
> 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2
> O 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 0 0 0 0
> b 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 0 0 0 0
> s 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 WANT
>
> 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
> 2 1 . . . . . . . . . . . . . . . . . . . . . . 1.
> 3 . . . . . . . . . . . . . . . 2 2 2 2 2 2 2 2 .2
> 4 1 1 1 1 1 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 132
> 5 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 12
> 6 1 1 1 1 3 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 132
> 7 1 1 3 3 3 3 2 2 2 2 2 2 . . 2 2 2 2 2 2 2 2 2 132.2
> 8 1 1 1 1 1 3 3 3 2 2 2 3 3 3 3 2 2 2 2 2 2 2 2 13232
> 9 1 1 1 1 3 3 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1321
> 10 1 1 1 3 3 3 3 2 2 2 2 2 1 1 3 2 2 2 2 2 2 2 2 132132
>
> I would like the new variable to indicate the changes in the order but not
> indicating how many years the given value occur each time.
Lene:
In order to replace all sequences of repeating characters with a
single instance of the character you can use PRXCHANGE with a
substitution regular expression pattern that takes advantage of
backtracking .
---------------------------------
data raw;
input y1-y23;
format y: 1.;
datalines;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . 2 2 2 2 2 2 2 2
1 1 1 1 1 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 3 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 3 3 3 3 2 2 2 2 2 2 . . 2 2 2 2 2 2 2 2 2
1 1 1 1 1 3 3 3 2 2 2 3 3 3 3 2 2 2 2 2 2 2 2
1 1 1 1 3 3 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1
1 1 1 3 3 3 3 2 2 2 2 2 1 1 3 2 2 2 2 2 2 2 2
run;
data raw_collapsed;
set raw;
retain _rx;
if _n_ = 1 then
_rx = prxparse ('s/(.)\1+/$1/');
length sequence $23;
sequence = cats(of y1-y23);
call prxchange (_rx, -1, sequence);
run;
---------------------------------
Note: If you are only concerned with removing repeated spaces, use the
COMPBL function.
--
Richard A. DeVenezia
http://www.devenezia.com