I wrote a macro that can recursively input the similar datasets into one data file.
It will be no problem for me to change the data name one by one if there are only several datasets.
However now, there are more than 100 datasets. How can I write a macro loop to read data names automatically?
For example:
%getdata(dataname) is the macro I wrote.
What I want to do is to
pesudecode:
%do name = A, B, C, D, E, F, G, ......
%getdata(name)
%end;
Thanks for the help in advance.
You can add another proc append after every macro be done.
data x;
input stock $;
cards;
A
B
C
D
;
run;
data _null_;
set x;
call execute('%getdata('||stock||')');
call execute('proc append base=want data=all force;run;');
run;
Ksharp
Try below.i haven't tested the program.
%let name=A,B,C,D,E.. ;
%let dataset=%scan(&name,1,',');
%do %while(&dataset ne '');
%let dataset=%scan(&name,&i,',');
%let i=%eval(&i+1);
%end;
Easy. Assuming all of your datasets are under C:\
libname x v9 'c:\'; data _null_; set sashelp.vmember(keep=libname memname where=(libname='X')); call execute('%getdata('||memname||')'); run;
Ksharp
Sorry, forgot to mention that. The datasets do not exists. They are the stock prices to be downloaded from Yahoo Finance. And A, B, C, D, E... are just stock codes.
%getdata(dataname), is to perform the download first, and recursively combine the data downloaded together in one file.
That's why I need to find a way to read the 100 stock codes automatically, rather than change them manually.
Thank you
what is the connection you use between yahoo finance and sas?
FILENAME myurl URL "http://ichart.finance.yahoo.com/table.csv?s=&tic..AX";
it's for australian stocks, so i put AX there. otherwise, you can leave it as ...=&tic"
If you already have a list of stock code. It is also easy.
data x; input stock $; cards; A B C D ; run; data _null_; set x; call execute('%getdata('||stock||')'); run;
Ksharp
It worked for the stock separately, which means the final data file only contains one stock only.
But I want the final data file containing all stocks fetched from Yahoo.
If I run %getdata(MCE) first, then it will generate a file called 'ALL', which contains MCE stock prices.
then run%getdata(BSL), then BSL data will be appended below MCE in 'ALL' recursively, and so on.
I dont know which part is not quite right.
Thanks
I think this code is useful, and I may redesign my own program and embed this to make it work.
Thank you very much.
Hi,
This is new thing for me. Which sas utility is requried to fecth data from website.
Nth is new, still the data step with one additional line with URL.
You can search Google that "download stock from Yahoo Finance in SAS", then you can find how it was done.
You can add another proc append after every macro be done.
data x;
input stock $;
cards;
A
B
C
D
;
run;
data _null_;
set x;
call execute('%getdata('||stock||')');
call execute('proc append base=want data=all force;run;');
run;
Ksharp
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.