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

hello all,

 

i'm new to using sas EG program.

im searching for a solution to export/generate  below data into multiple separate excel file depending on type (Car.xls, bus.xls bike.xls)

separate file.

 

can anyone suggest best approach it achevie the result.

 

Input data 

Type monthcost
Car01-01-20191000
Car01-02-20192000
Car01-03-20192000
Car01-04-20192000
Car01-05-20194000
Car01-06-20192000
Car01-07-20192000
Car01-08-20196000
Car01-09-20192000
Car01-10-20197000
Car01-11-20192000
Car01-12-20192600
bike01-01-20195000
bike01-02-20195000
bike01-03-20195000
bike01-04-20195000
bike01-05-20196333
bike01-06-20195777
bike01-07-20193000
bike01-08-20192354
bike01-09-20194300
bike01-10-20195000
bike01-11-20191222
bike01-12-20199000
bus01-01-20195000
bus01-02-20195000
bus01-03-20195000
bus01-04-20195000
bus01-05-20195000
bus01-06-20195000
bus01-07-20193000
bus01-08-20194000
bus01-09-20198000
bus01-10-20192000
bus01-11-20195000
bus01-12-20195000
truck01-01-20195000
truck01-02-20195000
truck01-03-20195000
truck01-04-20195000
truck01-05-20193000
truck01-06-20192900
truck01-07-20192900
truck01-08-20192900
truck01-09-20193999
truck01-10-20194000
truck01-11-20192000
truck01-12-20194000

 

Output file sample separate file

 

car excel file

Type monthcost
Car01-01-20191000
Car01-02-20192000
Car01-03-20192000
Car01-04-20192000
Car01-05-20194000
Car01-06-20192000
Car01-07-20192000
Car01-08-20196000
Car01-09-20192000
Car01-10-20197000
Car01-11-20192000
Car01-12-20192600

 

bus excel file

bus01-01-20195000
bus01-02-20195000
bus01-03-20195000
bus01-04-20195000
bus01-05-20195000
bus01-06-20195000
bus01-07-20193000
bus01-08-20194000
bus01-09-20198000
bus01-10-20192000
bus01-11-20195000
bus01-12-20195000
1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

I respond to your point in;

https://communities.sas.com/t5/SAS-Programming/SAS-Loop-over-dataset-for-create-export-files/m-p/591...

 

Yes, you can use call execute.  Call execute creates a string, which is inserted into the code after the executing block.  If that string is valid SAS code then it will run, otherwise you will get errors and warnings per any other SAS code.  From your screenshot

Capture.jpg

it is clear that there are spaces missing in the generated code which are causing the issues, this is due to the cats() function call.  Alter the code to:

proc sort data=have out=list nodupkey;
by type;
run;

data _null_; set have; call execute(cats('proc export data=have outfile="c:\',type,'.xlsx"; where type="',type,'"; run;')); run;

Where have is your input data.  This will create one proc export command for each unique type string.

View solution in original post

3 REPLIES 3
Haris
Lapis Lazuli | Level 10
One way to do this is to write separate EXPORT statements: WHERE Type='Car'/'bike'/'bus'/'truck';

Another common SAS processing workflow is to sort the data by Type and use BY TYPE statement. I am not sure if PROC EXPORT takes BY statement or not but SAS documentation should tell you that easily.
PaigeMiller
Diamond | Level 26

In the case where you have 3 or 4 levels, I would do what @Haris suggested and write 3 or 4 PROC EXPORTs with the proper WHERE= data set option. If the real problem has dozens or hundreds of levels, you could write a macro to do this, where first you use PROC SQL to determine what the levels are, and then loop through each level found and do the export.

--
Paige Miller
RW9
Diamond | Level 26 RW9
Diamond | Level 26

I respond to your point in;

https://communities.sas.com/t5/SAS-Programming/SAS-Loop-over-dataset-for-create-export-files/m-p/591...

 

Yes, you can use call execute.  Call execute creates a string, which is inserted into the code after the executing block.  If that string is valid SAS code then it will run, otherwise you will get errors and warnings per any other SAS code.  From your screenshot

Capture.jpg

it is clear that there are spaces missing in the generated code which are causing the issues, this is due to the cats() function call.  Alter the code to:

proc sort data=have out=list nodupkey;
by type;
run;

data _null_; set have; call execute(cats('proc export data=have outfile="c:\',type,'.xlsx"; where type="',type,'"; run;')); run;

Where have is your input data.  This will create one proc export command for each unique type string.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 3 replies
  • 1497 views
  • 1 like
  • 4 in conversation