Date: Wed, 28 May 2008 09:52:09 -0400
Reply-To: msz03@albany.edu
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Mike Zdeb <msz03@ALBANY.EDU>
Subject: Re: Many variables to one variable
Content-Type: text/plain;charset=iso-8859-1
hi ... another variation ...
data x (keep=id x1-x23 want);
input id (x1-x23) (: $char1.);
old = cat(of x1-x23);
do _n_ = 1 by 1;
want = tranwrd(old,'11','1');
want = tranwrd(want,'22','2');
want = tranwrd(want,'33','3');
want = tranwrd(want,'..','.');
if length(want) eq length(old) then leave;
old = want;
end;
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 1 . . . . . . . . . . . . . . . . . . . . . .
3 . . . . . . . . . . . . . . . 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
5 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
6 1 1 1 1 3 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
7 1 1 3 3 3 3 2 2 2 2 2 2 . . 2 2 2 2 2 2 2 2 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
9 1 1 1 1 3 3 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1
10 1 1 1 3 3 3 3 2 2 2 2 2 1 1 3 2 2 2 2 2 2 2 2
;
run;
--
Mike Zdeb
U@Albany School of Public Health
One University Place
Rensselaer, New York 12144-3456
P/518-402-6479 F/630-604-1475
> This might be good enough.
>
> data have;
> array y[*] Y1981-Y2003;
> input id y[*];
> attrib want length=$30;
> attrib cc length=$2;
> want = cats(of y[*]);
> do c = '1','2','3','.';
> cc = cats(c,c);
> do while(index(want,cc));
> want = tranwrd(want,cc,c);
> end;
> end;
> drop c cc;
> cards;
> 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 . . . . . . . . . . . . . . . . . . . . . .
> 3 . . . . . . . . . . . . . . . 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
> 5 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
> 6 1 1 1 1 3 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
> 7 1 1 3 3 3 3 2 2 2 2 2 2 . . 2 2 2 2 2 2 2 2 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
> 9 1 1 1 1 3 3 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1
> 10 1 1 1 3 3 3 3 2 2 2 2 2 1 1 3 2 2 2 2 2 2 2 2
> ;;;;
> run;
> Proc print;
> run;
>
> On 5/28/08, Lene Blenstrup <lenetb@socsci.aau.dk> 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.
>>
>> Thank you
>> Lene
>>
>
>
|