BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sweetgorgor
Calcite | Level 5
PortfolioValueNew_Value
A22
A33
A 3
A 3
B22
B22
B 2

Hi, I have a table with the  first 2 columns (Portfolio, Value) where I want to create a 3rd column to fill in all the blanks with the previous (Latest value), by portfolio. Essentially I am trying to derive column 3. I tried to use Lag and retain in a data step I can't seem to get it to work. can someone help?

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Mit
Calcite | Level 5 Mit
Calcite | Level 5

data have;
input portfolio $ value;
cards;
A 2
A 3


B 2


B 4


,
run;

proc sort data=have ;
by portfolio;
run;

data want;
set have;
by portfolio;
retain new_value;
if not missing(value) then new_value=value;
run;


portfolio


value


new_value


A


2


2


A


3


3


A


3


B


2


2


B


2


B


4


4


B


4

View solution in original post

3 REPLIES 3
Mit
Calcite | Level 5 Mit
Calcite | Level 5

data have;
input portfolio $ value;
cards;
A 2
A 3


B 2


B 4


,
run;

proc sort data=have ;
by portfolio;
run;

data want;
set have;
by portfolio;
retain new_value;
if not missing(value) then new_value=value;
run;


portfolio


value


new_value


A


2


2


A


3


3


A


3


B


2


2


B


2


B


4


4


B


4

sam369
Obsidian | Level 7

Hi,

another apporach, i used Mit Have dataset

data have;

input portfolio : $1. value;

cards;

A 2

A 3

B 2

B 4

;

run;

data want;

do until(last.portfolio);

set have;

by portfolio notsorted;

new_val=ifn(value ne .,value,new_val);

output;

end;

run;

Thanks

Sam

sweetgorgor
Calcite | Level 5

Thanks guys! I used Mit's and it worked like a charm! Sam yours also works! Thank you!

sas-innovate-2024.png

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.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 878 views
  • 6 likes
  • 3 in conversation