Macro resolved problem in an array

Accepted Solution Solved
Reply
Contributor
Posts: 51
Accepted Solution

Macro resolved problem in an array

I tried to read 12 monthly sas datasets from individual directory such as <path..>/Jan/jan2014.sas7dbdat and export to csv file in one public directory. 

It worked fine for one month like the code below:

options symbolgen mprint;

--------------------------------------

%let month = jan ;

libname inf "<input-pathname>/&month/" ;

%macro universal;

data test;

  set  inf.&month.2014;

<data manipulation>

  run;

proc export data=test;

     outfile="<output-path>/&month.2014.csv"

     dbms=csv replace;

run;

%mend;

data _null_;

%universal;

run;

------------------------------------------

BUT if I put inside an array, it resolved error:

data _null_;

  array monthin{12} Jan Feb ...... Nov Dec;

  do I=1 to 12;

      %let month = monthin{i} ;

      libname inf "<input-pathname>/&month/" ; 

      %universal ;

  end;

run;

My final result should be 12 csv files in a same output directory.

Thanks for your assistance.


Accepted Solutions
Solution
‎01-12-2015 01:50 PM
Grand Advisor
Posts: 10,211

Re: Macro resolved problem in an array

If you had MONTH as a parameter to your macro

AND include all of the bits than need MONTH inside the macro

you could pass that parameter using CALL Execute

%macro universal(month);

libname inf "<input-pathname>/&month/" ;

data test;

  set  inf.&month.2014;

<data manipulation>

  run;

proc export data=test;

     outfile="<output-path>/&month.2014.csv"

     dbms=csv replace;

run;

%mend;

and call with

%universal(Jan);

Then

data _null_;

  array monthin{12} Jan Feb ...... Nov Dec;

  do I=1 to 12;

      Call Execute ('%universal ('||monthin||')' ) ;

  end;

run;

View solution in original post


All Replies
Solution
‎01-12-2015 01:50 PM
Grand Advisor
Posts: 10,211

Re: Macro resolved problem in an array

If you had MONTH as a parameter to your macro

AND include all of the bits than need MONTH inside the macro

you could pass that parameter using CALL Execute

%macro universal(month);

libname inf "<input-pathname>/&month/" ;

data test;

  set  inf.&month.2014;

<data manipulation>

  run;

proc export data=test;

     outfile="<output-path>/&month.2014.csv"

     dbms=csv replace;

run;

%mend;

and call with

%universal(Jan);

Then

data _null_;

  array monthin{12} Jan Feb ...... Nov Dec;

  do I=1 to 12;

      Call Execute ('%universal ('||monthin||')' ) ;

  end;

run;

Grand Advisor
Posts: 17,342

Re: Macro resolved problem in an array

Use the libname function instead of the libname statement for that portion of your code.

You can then use call execute to call your macro.

An array references variables, so you're creating variables Jan-Dec but they have no values attached to them so you'll be passing missing values on. You need to use a temporary array instead.

A better way would be to have your macro take a single parameter and put the libname inside the macro.

Here's a sketch of the code. You can look up the call execute and see how to call your macro.

%macro export_csv(month);

libname inf "<input-pathname>/&month/" ;


data test;

  set  inf.&month.2014;

<data manipulation>

  run;

proc export data=test;

     outfile="<output-path>/&month.2014.csv"

     dbms=csv replace;

run;

libname inf;

%mend;

data _null_;

  array monthin{12} $ _temporary_  ("Jan" "Feb" ...... "Nov" "Dec");

  do I=1 to 12;

  

  *use call execute to call macro;

*this isn't valid SAS code you'll have to look up the exact syntax;

  call execute (%export_csv(month(i));

 

  end;

run;



Contributor
Posts: 51

Re: Macro resolved problem in an array

Thank you for all suggestions. I solved my problem  quickly based on your solutions.

Good luck to all contributors.

Regards,

William

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 315 views
  • 5 likes
  • 3 in conversation