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

Hi Everyone,

 

I have the following data:

 

data have;

input Sedol Date:ddmmyy10. Buy_volume Price;

format Date yymmdd10.;

cards;

2000001 2/01/1995 196000 0

2000001 10/02/1995 . .

2000001 27/02/1995 36000 4.55

2000001 2/03/1995 60000 4.31

2000001 13/03/1995 120000 4.51

2000002 2/01/1995 294000 0

2000002 10/02/1995 . .

2000002 27/02/1995 . .

2000002 28/02/1995 50000 14.2

2000002 13/03/1995 140000 11.6

2000002 20/03/1995 40000 11.6

2000002 22/03/1995 . .

2000003 2/01/1995 150000 0

2000003 28/02/1995 10000 13.6

2000003 24/05/1995 6000 13.5

2000003 5/06/1995 . .

2000003 18/07/1995 . .

2000003 25/02/1998 40000 30

2000003 6/04/1998 44000 31.88

;

 

For each Sedol, I need to fill up the first line in Price column with the first available data in the same column. Basically this is what I want:

 

SedolDateBuy_volumePrice
20000012/01/19951960004.55
200000110/02/1995..
200000127/02/1995360004.55
20000012/03/1995600004.31
200000113/03/19951200004.51
20000022/01/199529400014.2
200000210/02/1995..
200000227/02/1995..
200000228/02/19955000014.2
200000213/03/199514000011.6
200000220/03/19954000011.6
200000222/03/1995..
20000032/01/199515000013.6
200000328/02/19951000013.6
200000324/05/1995600013.5
20000035/06/1995..
200000318/07/1995..
200000325/02/19984000030
20000036/04/19984400031.88

 

Can someone please help? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

If you have SAS ETS, I believe PROC EXPAND can do this, otherwise here's relatively cumbersome approach.

 

data have;
input Sedol Date:ddmmyy10. Buy_volume Price;
format Date yymmdd10.;
cards;
2000001 2/01/1995 196000 0
2000001 10/02/1995 . .
2000001 27/02/1995 36000 4.55
2000001 2/03/1995 60000 4.31
2000001 13/03/1995 120000 4.51
2000002 2/01/1995 294000 0
2000002 10/02/1995 . .
2000002 27/02/1995 . .
2000002 28/02/1995 50000 14.2
2000002 13/03/1995 140000 11.6
2000002 20/03/1995 40000 11.6
2000002 22/03/1995 . .
2000003 2/01/1995 150000 0
2000003 28/02/1995 10000 13.6
2000003 24/05/1995 6000 13.5
2000003 5/06/1995 . .
2000003 18/07/1995 . .
2000003 25/02/1998 40000 30
2000003 6/04/1998 44000 31.88
;

data temp;
set have 
/*removes missing and price=0 records*/ (where =(not missing(price) and price ne 0));;
by sedol;
keep sedol price;
rename price = first_price;

*keeps only first for each sedol;
if first.sedol ;

run;



data want;
merge have (in=a)
      temp;
by sedol;
if a; *only if in Table Have;
if first.sedol and price=0 then price=first_price; *replace if necesssary;
drop first_price; *drop first value;
run;

 

 


@bd_user_10 wrote:

Hi Everyone,

 

I have the following data:

 

data have;

input Sedol Date:ddmmyy10. Buy_volume Price;

format Date yymmdd10.;

cards;

2000001 2/01/1995 196000 0

2000001 10/02/1995 . .

2000001 27/02/1995 36000 4.55

2000001 2/03/1995 60000 4.31

2000001 13/03/1995 120000 4.51

2000002 2/01/1995 294000 0

2000002 10/02/1995 . .

2000002 27/02/1995 . .

2000002 28/02/1995 50000 14.2

2000002 13/03/1995 140000 11.6

2000002 20/03/1995 40000 11.6

2000002 22/03/1995 . .

2000003 2/01/1995 150000 0

2000003 28/02/1995 10000 13.6

2000003 24/05/1995 6000 13.5

2000003 5/06/1995 . .

2000003 18/07/1995 . .

2000003 25/02/1998 40000 30

2000003 6/04/1998 44000 31.88

;

 

For each Sedol, I need to fill up the first line in Price column with the first available data in the same column. Basically this is what I want:

 

Sedol Date Buy_volume Price
2000001 2/01/1995 196000 4.55
2000001 10/02/1995 . .
2000001 27/02/1995 36000 4.55
2000001 2/03/1995 60000 4.31
2000001 13/03/1995 120000 4.51
2000002 2/01/1995 294000 14.2
2000002 10/02/1995 . .
2000002 27/02/1995 . .
2000002 28/02/1995 50000 14.2
2000002 13/03/1995 140000 11.6
2000002 20/03/1995 40000 11.6
2000002 22/03/1995 . .
2000003 2/01/1995 150000 13.6
2000003 28/02/1995 10000 13.6
2000003 24/05/1995 6000 13.5
2000003 5/06/1995 . .
2000003 18/07/1995 . .
2000003 25/02/1998 40000 30
2000003 6/04/1998 44000 31.88

 

Can someone please help? Thanks.



 

View solution in original post

3 REPLIES 3
Reeza
Super User

If you have SAS ETS, I believe PROC EXPAND can do this, otherwise here's relatively cumbersome approach.

 

data have;
input Sedol Date:ddmmyy10. Buy_volume Price;
format Date yymmdd10.;
cards;
2000001 2/01/1995 196000 0
2000001 10/02/1995 . .
2000001 27/02/1995 36000 4.55
2000001 2/03/1995 60000 4.31
2000001 13/03/1995 120000 4.51
2000002 2/01/1995 294000 0
2000002 10/02/1995 . .
2000002 27/02/1995 . .
2000002 28/02/1995 50000 14.2
2000002 13/03/1995 140000 11.6
2000002 20/03/1995 40000 11.6
2000002 22/03/1995 . .
2000003 2/01/1995 150000 0
2000003 28/02/1995 10000 13.6
2000003 24/05/1995 6000 13.5
2000003 5/06/1995 . .
2000003 18/07/1995 . .
2000003 25/02/1998 40000 30
2000003 6/04/1998 44000 31.88
;

data temp;
set have 
/*removes missing and price=0 records*/ (where =(not missing(price) and price ne 0));;
by sedol;
keep sedol price;
rename price = first_price;

*keeps only first for each sedol;
if first.sedol ;

run;



data want;
merge have (in=a)
      temp;
by sedol;
if a; *only if in Table Have;
if first.sedol and price=0 then price=first_price; *replace if necesssary;
drop first_price; *drop first value;
run;

 

 


@bd_user_10 wrote:

Hi Everyone,

 

I have the following data:

 

data have;

input Sedol Date:ddmmyy10. Buy_volume Price;

format Date yymmdd10.;

cards;

2000001 2/01/1995 196000 0

2000001 10/02/1995 . .

2000001 27/02/1995 36000 4.55

2000001 2/03/1995 60000 4.31

2000001 13/03/1995 120000 4.51

2000002 2/01/1995 294000 0

2000002 10/02/1995 . .

2000002 27/02/1995 . .

2000002 28/02/1995 50000 14.2

2000002 13/03/1995 140000 11.6

2000002 20/03/1995 40000 11.6

2000002 22/03/1995 . .

2000003 2/01/1995 150000 0

2000003 28/02/1995 10000 13.6

2000003 24/05/1995 6000 13.5

2000003 5/06/1995 . .

2000003 18/07/1995 . .

2000003 25/02/1998 40000 30

2000003 6/04/1998 44000 31.88

;

 

For each Sedol, I need to fill up the first line in Price column with the first available data in the same column. Basically this is what I want:

 

Sedol Date Buy_volume Price
2000001 2/01/1995 196000 4.55
2000001 10/02/1995 . .
2000001 27/02/1995 36000 4.55
2000001 2/03/1995 60000 4.31
2000001 13/03/1995 120000 4.51
2000002 2/01/1995 294000 14.2
2000002 10/02/1995 . .
2000002 27/02/1995 . .
2000002 28/02/1995 50000 14.2
2000002 13/03/1995 140000 11.6
2000002 20/03/1995 40000 11.6
2000002 22/03/1995 . .
2000003 2/01/1995 150000 13.6
2000003 28/02/1995 10000 13.6
2000003 24/05/1995 6000 13.5
2000003 5/06/1995 . .
2000003 18/07/1995 . .
2000003 25/02/1998 40000 30
2000003 6/04/1998 44000 31.88

 

Can someone please help? Thanks.



 

novinosrin
Tourmaline | Level 20


data have;
input Sedol Date:ddmmyy10. Buy_volume Price;
format Date yymmdd10.;
cards;
2000001 2/01/1995 196000 0
2000001 10/02/1995 . .
2000001 27/02/1995 36000 4.55
2000001 2/03/1995 60000 4.31
2000001 13/03/1995 120000 4.51
2000002 2/01/1995 294000 0
2000002 10/02/1995 . .
2000002 27/02/1995 . .
2000002 28/02/1995 50000 14.2
2000002 13/03/1995 140000 11.6
2000002 20/03/1995 40000 11.6
2000002 22/03/1995 . .
2000003 2/01/1995 150000 0
2000003 28/02/1995 10000 13.6
2000003 24/05/1995 6000 13.5
2000003 5/06/1995 . .
2000003 18/07/1995 . .
2000003 25/02/1998 40000 30
2000003 6/04/1998 44000 31.88
;


proc sql;
create table want as
select a.sedol,a.date,Buy_volume, ifn(min(date)=date,p,price) as Price
from
have a left join 
(select sedol, price as p from have where price not in (0,.) group by sedol having min(date)=date) b
on a.sedol=b.sedol
group by a.sedol
order by sedol,date;
quit;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

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
  • 682 views
  • 1 like
  • 3 in conversation