BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
michtka
Fluorite | Level 6


Hi everyone, how can I create the variable b from a.

a                b               

----            ------

1                1

1                1

2                2

2                2

3                3

4                4

4                4

1                5

1                5

2                6

3                7

4                8

4                8

Thnaks,

V

1 ACCEPTED SOLUTION

Accepted Solutions
DanielSantos
Barite | Level 11

Or use LAG function:

data want;

set have;

if lag1(A) ne A then B+1;

run;


More here:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/a000212547.htm

Cheers from Portugal.

Daniel Santos @ www.cgd.pt

View solution in original post

14 REPLIES 14
Tom
Super User Tom
Super User

You can use the NOTSORTED option on the BY statement.

data want ;

  set have;

  by a notsorted;

  b+first.a;

run;

DanielSantos
Barite | Level 11

Or use LAG function:

data want;

set have;

if lag1(A) ne A then B+1;

run;


More here:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/a000212547.htm

Cheers from Portugal.

Daniel Santos @ www.cgd.pt

MikeZdeb
Rhodochrosite | Level 12

hi ... or ...

data x;

set x;

b + (lag(a) ne a);

run;

DanielSantos
Barite | Level 11

Yep, same thing, different coding. That's a good alternative.

:smileylove: SAS!

Cheers from Portugal.

Daniel Santos @ www.cgd.pt

Howles
Quartz | Level 8

Often DIF leads to a more concise solution, vis a vis LAG. Not always.

   b ++ ^^ coalesce( dif( a ) , 1 ) ;

Linlin
Lapis Lazuli | Level 10

Hi Howles,

What are "++ ^^" in your code?

Thanks!

MikeZdeb
Rhodochrosite | Level 12

Hi ... I'll leave why use ++ to Howard (I've never understood why it's used).

As for ^^, it's a handy way to create 0/1 variables.  For example, what if you want to create a variable that has a value of 1 if certain text is found in a character string and 0 if not.  The FIND returns a location, not 1 or 0, but ...

data _null_;

text = 'abcde';

x1 = find(text,'c');

x2 = ^find(text,'c');

x3 = ^^find(text,'c');

put x1= x2= x3=;

run;

x1=3 x2=0 x3=1


X1 is 3, the location of 'c'

X2 is 0 since 3 is not 0 or missing

X3 is 1 since it's just not X2 (or X3 is 1 if text is found, otherwise 0)


That make sense?

Linlin
Lapis Lazuli | Level 10

Hi Mike,

Thank you for your explanation! I replaced "++" with "+" but I didn't see any difference in the output.

data have;

input a;

cards;

1              

1              

2               

2               

3               

4               

4               

1              

1               

2              

3               

4              

4

;

data want;

set have;

b + ^^ coalesce( dif( a ) , 1 ) ;

proc print;run;

mkeintz
PROC Star

The ^^ is functional, but I believe the "++" is strictly a mnemonic device, to be visually symmetric to:

     b +-   (expression)

Functionally

    b ++ (expression)

is the same as

   b + (expression)

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Linlin
Lapis Lazuli | Level 10

Thank you! That is what I thought.

Howles
Quartz | Level 8

The first + is part of the syntax for the assignment statement. The second is a bit of dorfmania and basically does nothing. If I were really striving for carpenterian job security I could have made it

   ++ --  ^^

MikeZdeb wrote:

Hi ... I'll leave why use ++ to Howard (I've never understood why it's used).

As for ^^, it's a handy way to create 0/1 variables.  For example, what if you want to create a variable that has a value of 1 if certain text is found in a character string and 0 if not.  The FIND returns a location, not 1 or 0, but ...

data _null_;

text = 'abcde';

x1 = find(text,'c');

x2 = ^find(text,'c');

x3 = ^^find(text,'c');

put x1= x2= x3=;

run;

x1=3 x2=0 x3=1


X1 is 3, the location of 'c'

X2 is 0 since 3 is not 0 or missing

X3 is 1 since it's just not X2 (or X3 is 1 if text is found, otherwise 0)


That make sense?

Haikuo
Onyx | Level 15

If using DOW, then b is actually '_n_':

data want;

  do until (last.a);

    set have;

       by a notsorted;

     b=_n_;

     output;

  end;

run;

Haikuo

MikeZdeb
Rhodochrosite | Level 12

hi ... nice, then you can also try ...

data want;

do b=_n_ by 0 until (last.a);

  set have;

  by a notsorted;

  output;

end;

run;

Haikuo
Onyx | Level 15

I like the use of 'by 0', quite neat. Thanks, Mike.

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 14 replies
  • 4548 views
  • 8 likes
  • 8 in conversation