BookmarkSubscribeRSS Feed
mmea
Quartz | Level 8

Hi.

 

I have a code that makes 25 CSV files.

These files are based on 5 regions and each region has 5 files.

I want to create 5 zipfiles for each regions, which includes the 5 files for each region.

 

I use this code to export to csv file (the 25 files), i use it in a macro to get the name of the file, so i my folder i get 25 files looking like this:

 

A_COVID19_TAB1_20210317.csv

A_COVID19_TAB2_20210317.csv

A_COVID19_TAB3_20210317.csv

A_COVID19_TAB4_20210317.csv

A_COVID19_TAB5_20210317.csv

 

B_COVID19_TAB1_20210317.csv

B_COVID19_TAB2_20210317.csv

B_COVID19_TAB3_20210317.csv

B_COVID19_TAB4_20210317.csv

B_COVID19_TAB5_20210317.csv

....

same for regioncode C, D, E

 

I want to make 5 zip files for each code named:

A_COVID19_20210317.zip

B_COVID19_20210317.zip

C_COVID19_20210317.zip

D_COVID19_20210317.zip

E_COVID19_20210317.zip

 

%let YYYYMMDD=%sysfunc(inputn(&DDMMMYYYY_h,best12.), YYMMDDN.6);

%macro regioncode(region);
%do i=1 %to 5;

proc export data=tabel&i. replace
  outfile="&out_folder.\&region._COVID19_TAB&i._&YYYYMMDD..csv"
  dbms=dlm
;
delimiter=';';
quit;


%regioncode(A);
%regioncode(B);
%regioncode(C);
%regioncode(D);
%regioncode(E);


%end;
%mend;


 

5 REPLIES 5
Kurt_Bremser
Super User

You can write your csv directly as a member into a ZIP file:

filename zipfile zip '/folders/myfolders/myfile.zip';

data _null_;
set sashelp.class;
file zipfile(class.csv) dlm=";" dsd;
if _n_ = 1 then put "name;sex;age";
put name sex age;
run;

data _null_;
set sashelp.cars;
file zipfile(cars.csv) dlm=";" dsd;
if _n_ = 1 then put "make;model";
put make model;
run;

filename zipfile clear;

Note that the

fileref(member)

notation cannot be used in PROC EXPORT (it accepts only physical filenames), so you have to use DATA _NULL_ steps to write the csv content.

mmea
Quartz | Level 8

Can I do this in the same Macro as shown ?

mmea
Quartz | Level 8

I have table 1 to 5 - they all contain different data/varables but same structure and formats

Kurt_Bremser
Super User

That is a contradiction. If they have different variables, they can't have the same structure. "Same structure" means the same variables with the same types and attributes.

 


@mmea wrote:

I have table 1 to 5 - they all contain different data/varables but same structure and formats


 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 607 views
  • 0 likes
  • 2 in conversation