cusip prc m d wk mark
111 -23 6 28 58 1
111 20 6 27 58 .
111 22 6 26 58 .
111 24 6 25 58 .
111 27 6 24 57 .
111 21.5 5 30 57 .
111 25 5 29 57 .
111 24 5 28 57 .
111 27 5 27 57 .
222 -24 5 31 57 1
222 27 5 30 57 .
222 22.5 5 29 56 .
222 29 5 28 56 .
333 -25 4 30 56 1
333 27 4 29 56 .
333 23.5 4 28 56 .
333 26 4 27 55 .
333 29 4 26 55 .
and i want the output like below
cusip prc m d wk mark new_mark
111 -23 6 28 58 1 1
111 20 6 27 58 . 1
111 22 6 26 58 . 1
111 24 6 25 58 . 1
111 27 6 24 57 . .
111 21.5 5 30 57 . .
111 25 5 29 57 . .
111 24 5 28 57 . .
111 27 5 27 57 . .
222 -24 5 31 57 1 1
222 27 5 30 57 . 1
222 22.5 5 29 56 . .
222 29 5 28 56 . .
333 -25 4 30 56 1 1
333 27 4 29 56 . 1
333 23.5 4 28 56 . 1
333 26 4 27 55 . .
333 29 4 26 55 . .
if mark=1 then i want the same cusip and same wk have new_mark value =1
thanks!!
data have;
infile cards dsd;
input cusip $ prc m d wk mark;
cards;
111,-23,6,28,58,1
111,20,6,27,58,.
111,22,6,26,58,.
111,24,6,25,58,.
111,27,6,24,57,.
111,21.5,5,30,57,.
111,25,5,29,57,.
111,24,5,28,57,.
111,27,5,27,57,.
222,-24,5,31,57,1
222,27,5,30,57,.
222,22.5,5,29,56,.
222,29,5,28,56,.
333,-25,4,30,56,1
333,27,4,29,56,.
333,23,.54,28,56,.
333,26,4,27,55,.
333,29,4,26,55,.
;
run;
data want;
retain new_mark;
set have;
by cusip descending wk;
if first.cusip then new_mark = mark;
if first.wk then new_mark = mark;
run;
The method would allow you to carry forward more variables more easily. And makes it easier if you want to keep the variable name.
thank you!! now i can continue my reserch
but have a little confuse about the code logic,can you just say some? Please!
thank you
You should look at documentation for UPDATE statement to understand exactly what it is doing. We are hijacking one feature of UPDATE and overriding another.
Thank you! i have try to documentation for UPDATE statement,just not familiar with it!
Anyway,thanks for your help, i will keep practice it
I've been seeing update a lot and I think I'm missing out on it, do you have a favorite document, sug / training material that you could point me in the direction of?
Cheers,
Mark
LetsGoBucs
With Proc SQL this is quite easy:
proc sql;
select *,
max(mark) as new_mark
from have
group by cusip, wk;
quit;
Thank you! I got it!
Data Step is also easy.
data have; infile cards dsd; input cusip $ prc m d wk mark; cards; 111,-23,6,28,58,1 111,20,6,27,58,. 111,22,6,26,58,. 111,24,6,25,58,. 111,27,6,24,57,. 111,21.5,5,30,57,. 111,25,5,29,57,. 111,24,5,28,57,. 111,27,5,27,57,. 222,-24,5,31,57,1 222,27,5,30,57,. 222,22.5,5,29,56,. 222,29,5,28,56,. 333,-25,4,30,56,1 333,27,4,29,56,. 333,23,.54,28,56,. 333,26,4,27,55,. 333,29,4,26,55,. ; run; data want; set have; by cusip wk notsorted; retain new_mark; if first.wk then new_mark=.; if not missing(mark) then new_mark=mark; run;
Xia Keshan
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.