Hi ,
I am looking for a macro code as i have a variable name city and that variable contains 126 names of unique cities,
and want to export the same data set into excel with condition where city='xyz'
and the excel should contain the name of the same city which is used in the condition.
As using the following code for 126 unique cities is not affordable.
e.g.: proc export
data=WORK.CAPFIRST_ENCOLLEC_MASTER (where=(City='PUNE'))
dbms=xlsx
outfile="\\10.4.1.205\Upload\DAC_FINAL_PUNE.xlsx"
replace;
run;
So looking for macro and looping to export using that macro.
Does this work as expected - it should create two workbooks, one for males and one for females?
If so, use this as the template for your code.
proc sql;
create table distinct_sex as
select distinct sex from sashelp.class;
quit;
%macro export_sheet(sex=);
proc export data=sashelp.class (where=(sex="&sex"))
outfile="C:\_localdata\sex_&sex..xlsx" dbms=xlsx replace; run;
%mend;
data _null_;
set distinct_sex;
str = catt('%export_sheet(sex=', sex, ');');
call execute (str);
run;
Try using ODS EXCEL instead. You can tell it to make a separate sheet for each BY group.
@anirudhs wrote:
Hi ,
I am looking for a macro code as i have a variable name city and that variable contains 126 names of unique cities,
and want to export the same data set into excel with condition where city='xyz'
and the excel should contain the name of the same city which is used in the condition.
As using the following code for 126 unique cities is not affordable.
e.g.: proc export
data=WORK.CAPFIRST_ENCOLLEC_MASTER (where=(City='PUNE'))
dbms=xlsx
outfile="\\10.4.1.205\Upload\DAC_FINAL_PUNE.xlsx"
replace;
run;
So looking for macro and looping to export using that macro.
Are you trying to create a new workbook for each city or a new worksheet for each city?
If it's a new worksheet, then use the ODS EXCEL with BY option as indicated by @Tom
If a new workbook, then you would need another option. Rather than a macro, for something this simple, I would recommend Call Execute.
You don't really need a macro. You just need to use code generation. Since you a driving the code generation from data it is probably easier to do with a data step. You could use CALL EXECUTE().
data _null_;
set WORK.CAPFIRST_ENCOLLEC_MASTER ;
by city;
if first.city ;
call execute(catx(' '
,'proc export'
,'data=WORK.CAPFIRST_ENCOLLEC_MASTER dbms=xlsx'
,'outfile=',quote(cats('\\10.4.1.205\Upload\DAC_FINAL_',city,'.xlsx'))
,'replace;'
,'where City=',quote(trim(city)),';'
,'run;'
));
run;
@anirudhs wrote:
yes i want new work book for each city.
but the code and condition is to be written 126 times... is that fisible ????
to write the proc export code 126 times by just changing the city name in each occurrence of export code
No one suggested you do that. We asked what you were trying to do, so we can provide a better response.
You can make the code be generated automatically, see @Toms answer. A macro would work but it's more code.
1. First get a list of all citys
2. Write a macro to export
3. Call macro for each city using call execute.
Untested below:
proc sql;
create table distinct_city as
select distinct city from have;
quit;
%macro export_sheet(city=);
proc export data=&have (where=(city="&city"))
outfile="C:\_localdata\&City..xlsx" dbms=xlsx replace; run;
%mend;
data _null_;
set distinct_city;
str = catt('%export_sheet(city=', city, ');');
call execute (str);
run;
Personally, I'd use Tom's solution instead.
300K rows is trivial. If it can fit in Excel its not big data 🙂
@anirudhs wrote:
@Tom getting following errors for all cities when testing your code :
NOTE: Line generated by the CALL EXECUTE routine.
89 + data=WORK.DAC_3 dbms=xlsx outfile= "\\10.4.1.205\Upload\DAC_FINAL_YAMUNANAGAR.xlsx" replace;
____
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
Most likely you have a missing or extra semi-colon. Would need to see the whole log to debug, but you should be able to debug on your own. Get the syntax for one proc step right and working and then make sure the data step is generating the same syntax.
How many XLSX files are you generating?
Why are you generating so many XLSX files?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.