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
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
You can use the NOTSORTED option on the BY statement.
data want ;
set have;
by a notsorted;
b+first.a;
run;
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
hi ... or ...
data x;
set x;
b + (lag(a) ne a);
run;
Yep, same thing, different coding. That's a good alternative.
:smileylove: SAS!
Cheers from Portugal.
Daniel Santos @ www.cgd.pt
Often DIF leads to a more concise solution, vis a vis LAG. Not always.
b ++ ^^ coalesce( dif( a ) , 1 ) ;
Hi Howles,
What are "++ ^^" in your code?
Thanks!
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?
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;
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)
Thank you! That is what I thought.
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?
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
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;
I like the use of 'by 0', quite neat. Thanks, Mike.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.