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

 

I am new to SAS and have this basic problem. I have a list of NYSE trading dates in table A as follows -

trading_date

1st March 2012

2nd March 2012

3rd March 2012

4th March 2012

5th March 2012

6th March 2012

 

I have another table B that has share price information as -

Date ID Ret Price

1st March 2012 1 … …

3rd March 2012 1 … …

4th March 2012 1 … …

5th March 2012 1 … …

6th March 2012 1 … …

1st March 2012 2 … …

3rd March 2012 2 … …

4th March 2012 2 … …

... has numeric data related to price and returns.

 

Now I need to join the NYSE Data table to the above table to get the following table -

Date ID Ret Price

1st March 2012 1 … …

2nd March 2012 1 0 0

3rd March 2012 1 … …

4th March 2012 1 … …

5th March 2012 1 … …

6th March 2012 1 … …

1st March 2012 2 … …

2nd March 2012 2 0 0

3rd March 2012 2 … …

4th March 2012 2 … …

 

i.e. a simple left join. The zero's will be filled with . in SAS to indicate missing values, but you get the idea. But if I use the following command -

 

proc sql;
	create table joined as select table_a.trading_date, 
		table_b.* from table_a 
		LEFT OUTER join table_b
		on table_a.trading_date=table_b.date;
quit;

 

The join happens only for the first ID (i.e. ID=1) while for the rest of the IDs, the same data is maintained. But I need to insert the trade dates for all IDs.

 

How can get the final data without running a do while loop for all IDs? I have 1000 IDs and looping and joining 1000 times is not an option due to limited memory.

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

If you want all observations from either than do: 

 

proc sql;
     create table want as
     select     COALESCE(FIRST_TABLE.ID,SECOND_TABLE.ID) as ID,
                    ...
     from       FIRST_TABLE
     full join     SECOND_TABLE
     on          FIRST_TABLE.ID=SECOND_TABLE.ID;
quit;
 

 

The full join means all rows from both tables are merged on id giving full list, you do need the coalesce to get id populated, as if you just take first table then it would be missing for those which appear only in second table.

 

Another approach from @PGStats:

You should use a CROSS JOIN to get all combinations of ID and trading_date and then LEFT JOIN to your trading data :

 

 

proc sql;
create table joined as
select I.ID, a.trading_date as date, b.price
from
  (select unique ID from b) as I cross join
  a left join
  b on I.ID=b.ID and a.trading_date=b.date
order by ID, date;
quit;

View solution in original post

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

If you want all observations from either than do: 

 

proc sql;
     create table want as
     select     COALESCE(FIRST_TABLE.ID,SECOND_TABLE.ID) as ID,
                    ...
     from       FIRST_TABLE
     full join     SECOND_TABLE
     on          FIRST_TABLE.ID=SECOND_TABLE.ID;
quit;
 

 

The full join means all rows from both tables are merged on id giving full list, you do need the coalesce to get id populated, as if you just take first table then it would be missing for those which appear only in second table.

 

Another approach from @PGStats:

You should use a CROSS JOIN to get all combinations of ID and trading_date and then LEFT JOIN to your trading data :

 

 

proc sql;
create table joined as
select I.ID, a.trading_date as date, b.price
from
  (select unique ID from b) as I cross join
  a left join
  b on I.ID=b.ID and a.trading_date=b.date
order by ID, date;
quit;
PGStats
Opal | Level 21

You should use a CROSS JOIN to get all combinations of ID and trading_date and then LEFT JOIN to your trading data :

proc sql;

create table joined as

select I.ID, a.trading_date as date, b.price

from

  (select unique ID from b) as I cross join

  a left join

  b on I.ID=b.ID and a.trading_date=b.date

order by ID, date;

quit;

PG

PG

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
  • 2 replies
  • 105620 views
  • 1 like
  • 3 in conversation