Date: Mon, 1 Sep 2008 18:35:31 -0400
Reply-To: Arthur Tabachneck <art297@NETSCAPE.NET>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Arthur Tabachneck <art297@NETSCAPE.NET>
Subject: Re: Create a new variable
Howard,
Very nice! As I have never used the notsorted option, I had to look it
up. While perusing the docs for what notsorted does, I also noticed
another option I have never used: groupformat.
I wonder how often it could be used to simplify solutions.
Art
-------
On Mon, 1 Sep 2008 17:50:43 -0400, Howard Schreier <hs AT dc-sug DOT org>
<schreier.junk.mail@GMAIL.COM> wrote:
>On Mon, 1 Sep 2008 09:10:19 -0400, Arthur Tabachneck <art297@NETSCAPE.NET>
>wrote:
>
>>Yom,
>>
>>You have one additional record in your example desired output but, if
that
>>was just an oversight, the following might accomplish the task:
>>
>>data want;
>> set have;
>> retain changed last_var2;
>> by var0;
>> if first.var0 then do;
>> var3=1;
>> changed=0;
>> last_var2=var2;
>> end;
>> else if not(changed) then do;
>> if var2 eq last_var2 then var3=1;
>> else do;
>> changed=1;
>> var3=0;
>> end;
>> end;
>> else var3=0;
>>run;
>>proc print data=want;
>>run;
>>
>>HTH,
>>Art
>
>Or
>
> data want;
> set have;
> by var0 var2 notsorted;
> retain var3;
> if first.var0 then var3 = 1;
> else if first.var2 then var3 = 0;
> run;
>
>>---------
>>On Mon, 1 Sep 2008 10:27:55 +0200, yom <yomsas@GMAIL.COM> wrote:
>>
>>>Dear all,
>>>
>>>I would like to create a new variable in a table according some
>>conditions.
>>>The table is sorted by var0 and var1.
>>>For each new first value of var0, i give the value 1 to the variable
var3.
>>>If var2 keep its first value many times, we have var3 = 1 for each one.
>>And
>>>as soon as var2 has a new value, we have var3 = 0 for the other
>>observations
>>>for which var0 has the same value.
>>>
>>>With the example below, for the first observation for wich var0 = 1, we
>>can
>>>see that var2=4. Since this value occurs three times in a row, we have
>>>var3=1 for the three first observations and var3=0 for the others (even
if
>>>var2=4 at the seventh observation).
>>>And, i do the same for the first observation for wich var0 = 2.
>>>
>>>It is not easy to explain. I hope that the example below will be useful.
>>>
>>>var0 var1 var2
>>> 1 2 4
>>> 1 3 4
>>> 1 4 4
>>> 1 5 1
>>> 1 6 6
>>> 1 7 4
>>> 1 8 7
>>> 2 1 3
>>> 2 2 3
>>> 2 3 2
>>> 2 4 9
>>> 2 5 8
>>> 2 6 3
>>> 2 7 2
>>> 3 3 7
>>> 3 4 7
>>> 3 5 7
>>> 3 6 3
>>> 3 7 1
>>> 3 8 7
>>> 3 9 7
>>>
>>>I would like to create the variable var3 according to :
>>>
>>>var0 var1 var2 var3
>>> 1 2 4 1
>>> 1 3 4 1
>>> 1 4 4 1
>>> 1 5 1 0
>>> 1 6 6 0
>>> 1 7 4 0
>>> 1 8 7 0
>>> 2 1 3 1
>>> 2 2 3 1
>>> 2 3 2 0
>>> 2 4 9 0
>>> 2 5 8 0
>>> 2 6 3 0
>>> 2 7 2 0
>>> 3 3 7 1
>>> 3 4 7 1
>>> 3 5 7 1
>>> 3 6 3 0
>>> 3 7 1 0
>>> 3 8 7 0
>>> 3 9 7 0
>>> 3 10 2 0
>>>
>>>
>>>Thank you very much in advance for your answers.
>>>
>>>Yom
|