Hi, I've been downloading quarterly panel data from Datastream. The data looks something like below
FirmID _1_Jan_95 _1_Feb_95....................... _1_Dec_10... _1_Apr_11
A 8.4 52.4 252 54.2
B 45.6 52.4 234 85.4
...
I wrote a macro to import all Excel sheets into SAS, each sheet being a separate SAS dataset. Then, the macro clean invalid values, converts char-to-num values for these values (it's numeric but after importing SAS makes it char instead of num), append/stack up all these datasets on top of each other, and finally transpose to panel data structure that looks like below
FirmID Date VariableName
A _1_Jan_95 8.4
A _1_Feb_95 52.4
...
I have to write a long list of dates to loop over. For example,
%let columnname= _1_Jan_95 _1_Feb_95............................. - 1_Apr_2011;
Based on this, I looped over this using %SCAN(&columname, &i) in cleaning, converting data.
I found out that sometimes I mistyped the columnname. Say, instead of _1_Jul_95 I mistyped it to be 1_Jul_1995 or _1_July_95. It's annoying to make this typing errors.
So I created a macro to generate a variable list like below
/************************************************************************/
/* A macro to generate a long list of variable names typically found in */
/* a Worldscope/Datastream data set after being imported into SAS */
/* Example: After importing a DS/WC Excel sheet into SAS, Column 1 looks*/
/* like below: */
/* _1_Mar_2008 */
/* gen_vars accepts 5 arguments */
/* - day=: the day displayed like 1 above */
/* - mon=: a list of months of data for a given year, say, Mar, Jun, Dec*/
/* - mon_num: the number of months above, say 3 for months above*/
/* - year= a list of years data collected over, say 1995 1996, etc... */
/* - year_num=: the number of years above */
/************************************************************************/
%macro gen_vars (day=, mon=, mon_num=, year=, year_num=);
%local i j;
%do i=1 %to &year_num %by 1; /*Loop over years*/
%do j=1 %to &mon_num %by 1; /*Loop over months*/
%put _&day._%scan(&mon, &j)_%scan(&year, &i);
%end;
%end;
%mend gen_vars;
MY TROUBLE is, I don't know how to assign the RETURN VALUES from this gen_vars macro. In Matlab/R, when I created a function with a return value and if I want to assign the value returned by the function, I simply do sth like: myvar = myfunction(argument), and that's it, the return value of myfunction(argument) is assigned to myvar.
But in SAS, how can I do this? Can I do the following?
%let mon=Jan Mar Jun Sep Dec;
%let year=95 96 97 98 99 00 01 02 03 04 05 06 07 08 09 10 11;
%let varlist = %gen_vars(day=1, mon=&mon, mon_num=4, year=&year, year_num=17);
I've tried this but it gives errors, but I can't figure out how it is wrong and where.
Can anyone please help?
Thanks so much for your time.