DATA Step, Macro, Functions and more

How to import income statement info from Yahoo!

Reply
Super Contributor
Posts: 413

How to import income statement info from Yahoo!

Hi,

 

I am trying to import income statement info for the stock MNKD, here is the link:

https://ca.finance.yahoo.com/q/is?s=MNKD&annual

 

I would like to download the income statement info, but when I try do get some elements, I inspect an element and get its id and then put this id in my code, but all I get is hundreds of empty rows.

 

Is there a way to get this kind of data?

 

Thank you!

Super User
Posts: 17,818

Re: How to import income statement info from Yahoo!

Please post the code you're trying to use. 

Super Contributor
Posts: 413

Re: How to import income statement info from Yahoo!

[ Edited ]

Hi Reeza,

 

here is the code:

 

data the_stocks;
input symbol$;
datalines;
MNKD
;
run;

proc sql noprint ;
select symbol
into :symbol separated by ' '
from the_stocks;
quit;


%macro stocks(all_data);
%let k=1;

%let symbol= %scan(&all_data, &k);
%do %while("&symbol" NE "");
filename &symbol url " https://ca.finance.yahoo.com/q/is?s=&symbol&annual " debug;
data &symbol ;
infile &symbol dsd lrecl = 128 firstobs=2;
input yfnc_tabledata1 ;
format stock $5.;
stock = "&symbol";
run;

%let k = %eval(&k + 1);
%let symbol = %scan(&all_data, &k);

%end;
%mend;

%stocks(&symbol)

 

yfnc_tabledata1 is the html id of the table which I am trying to import.

 

Thank you!

Super User
Super User
Posts: 7,401

Re: How to import income statement info from Yahoo!

As with all posts like this, you are trying to run before you can walk.  Remove all the macro stuff, get some Base SAS code working first - this is the programming language after all not macro.  So lets look at the basic code:

filename temp url "https://ca.finance.yahoo.com/q/is?s=MNKD&annual" debug;
data kknd;
  infile temp dsd lrecl=128 firstobs=2;
  input yfnc_tabledata1 ;
  format kknd $5.;
  stock = "kknd";
run;

So, first line - you have put " around the string, this triggers the macro processor to replace macro variables, &annual isn't a macro variable but because its within " it gets treated like it.  That is the first error.  Second error, you are inputting to a numeric variable yfnc_tabledata1, however the page you are looking for doesn't include a file or one string, it is a html page with javascript to retrieve data.  Third problem, kknd is never used anywhere?

 

So I am afraid to say, this whole process you have here is not going to work.  

Super Contributor
Posts: 413

Re: How to import income statement info from Yahoo!

Hi RW9,

 

I prevously obtained the daily prices for the stock in a similar way and it actually worked!

1) Concerning the "&annual", in my code for stock prices I had even more &s:

filename &symbol url " http://ichart.finance.yahoo.com/table.csv?s=&symbol&d=11&e=2&f=2015&g=d&a=9&b=1&c=2015&ignore=.csv " debug;

 

2) Format stock $5. is actually a new column that I included that contians the stock's ticker like this when I append all the stocks data together I know which data belongs to which stock.

 

3) I had "input date adjclose", and like this got only the date and its adjusted close price, since date and adjclose are html elements.

Now I am tryig to get the entire table, but as I already mentioned I get empty rows...

 

Thank you! 

Super User
Posts: 9,676

Re: How to import income statement info from Yahoo!

You are connecting HTTPS . You can't get any result unless you configure something at SAS side. Check the documentation of proc http.

 

Alternative way is using CURL to get data and import it by SAS.

Super Contributor
Posts: 413

Re: How to import income statement info from Yahoo!

Hi Ksharp,

 

I actually already imported daily stock price and my code, similar to this one worked. The web page of the daily prices was also https, but in my code I used only http and it still worked.

It seems to me that what I am inputting is wrong, but I might be mistaken....

 

Thank you!

Super User
Posts: 9,676

Re: How to import income statement info from Yahoo!

That is odd. When running the following code. I get Time Out ERROR. I can't get any data for it due to HTTPS.

 

filename temp url "https://ca.finance.yahoo.com/q/is?s=MNKD&annual" debug;
data _null_;
  infile temp ;
  input ;
  put _infile_;
run;

 

 

Super Contributor
Posts: 413

Re: How to import income statement info from Yahoo!

[ Edited ]

Hi KSharp,

 

I also ran your code and got an empty table. Looking at the log, it seems that it actually shows the html text. I can actually get the html with the following code:

 

 data download;
length view $32767;
filename the_vid url "http://ca.finance.yahoo.com/q/is?s=MNKD&annual";
infile the_vid lrecl=32767;
input;
view = _infile_;
run;

 

The income statement info is actually present in the table "download", but its a mess to get it out...

 

That's why I thought if it were possible to get the income statement table in table form, just like it is possible to get the daily price in table form. But on the other hand, the daily price is actually a csv file, whereas the income statement it raw html.

 

Thank you!

Super User
Posts: 9,676

Re: How to import income statement info from Yahoo!

Still got error, Maybe there is a problem at my side, I am in China. 

 

34 data download;
35 length view $32767;
36 filename the_vid url
36 ! 'http://ca.finance.yahoo.com/q/is?s=MNKD&annual';
37 infile the_vid lrecl=32767;
38 input;
39 view = _infile_;
40 run;

ERROR: 10060 - WSAETIMEDOUT.
NOTE: The SAS System stopped processing this step because of
errors.
WARNING: The data set WORK.DOWNLOAD may be incomplete. When
this step was stopped there were 0 observations and 1
variables.
NOTE: DATA statement used (Total process time):
real time 21.02 seconds
cpu time 0.09 seconds

 

Super Contributor
Posts: 413

Re: How to import income statement info from Yahoo!

I am using SAS 9.3, maybe you are using a different version?

Super User
Posts: 9,676

Re: How to import income statement info from Yahoo!

Tried it in both UE and 9.2 .

Here is some basic idea, you can start out with it.

 

 filename the_vid url 'http://www.sas.com';

data download;
infile the_vid recfm=n dsd dlm='<>';
input view : $200. @@;
run;
 
Super Contributor
Posts: 413

Re: How to import income statement info from Yahoo!

I ran the code, it indeed did some cleaning to the html.

 

I was wandering if it is possible to download specific html elements and not the whole html of the page. In my case the class of the table that I try to download is yfnc_tabledata1, bu when I input it in the code I get a variable yfnc_tabledata1 but with empty rows.

 

Thank you!

Super User
Posts: 9,676

Re: How to import income statement info from Yahoo!

Sorry. I can't get any data from that url, so I can't do any further. It is on your own.

Super Contributor
Posts: 413

Re: How to import income statement info from Yahoo!

It is indeed quite complicated...

 

Thank you very much for your time and effort!

Ask a Question
Discussion stats
  • 14 replies
  • 610 views
  • 3 likes
  • 4 in conversation