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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

View solution in original post

11 REPLIES 11
manojinpec
Obsidian | Level 7

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;

Ksharp
Super User

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

Fred_Gavin
Calcite | Level 5

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

manojinpec
Obsidian | Level 7

what is the connection you use between yahoo finance and sas?

Fred_Gavin
Calcite | Level 5

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"

Ksharp
Super User

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

Fred_Gavin
Calcite | Level 5

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

Fred_Gavin
Calcite | Level 5

I think this code is useful, and I may redesign my own program and embed this to make it work.

Thank you very much.

manojinpec
Obsidian | Level 7

Hi,

This is new thing for me. Which sas utility is requried to fecth data from website.

Fred_Gavin
Calcite | Level 5

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.

Ksharp
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 11 replies
  • 1763 views
  • 3 likes
  • 3 in conversation