J'ai finalement pu trouver une solution pour générer un fichier avec plusieurs onglets et exporter mes données dedans.
1° Pour créer le fichier excel avec la structure souhaitée:
ici c'est un classeur simple avec deux onglets.
Vous pouvez créer un fixhier excel avec la mise en forme souhaitée et récupérer le code XML pour le réutiliser dans l'étape data ci-dessous.
Pour récupérer le code XML:
Dans votre fihier excel => File => Save As (XML) => Ouvrer le fichier XML dans blocnote.
Code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| filename toxls "&PATHOUT.&FIC..xls";
DATA _null_ ;
file toxls ;
put '<?xml version="1.0"?>';
put '<?mso-application progid="Excel.Sheet"?>';
put '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"';
put ' xmlns:o="urn:schemas-microsoft-com:office:office"';
put ' xmlns:x="urn:schemas-microsoft-com:office:excel"';
put ' xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"';
put ' xmlns:html="http://www.w3.org/TR/REC-html40">';
put ' <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">';
put ' <LastAuthor>dumontsy</LastAuthor>';
put ' <Created>1996-10-14T23:33:28Z</Created>';
put ' <Version>11.9999</Version>';
put ' </DocumentProperties>';
put ' <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">';
put ' <WindowHeight>9300</WindowHeight>';
put ' <WindowWidth>15135</WindowWidth>';
put ' <WindowTopX>120</WindowTopX>';
put ' <WindowTopY>120</WindowTopY>';
put ' <AcceptLabelsInFormulas/>';
put ' <ProtectStructure>False</ProtectStructure>';
put ' <ProtectWindows>False</ProtectWindows>';
put ' </ExcelWorkbook>';
put ' <Styles>';
put ' <Style ss:ID="Default" ss:Name="Normal">';
put ' <Alignment ss:Vertical="Bottom"/>';
put ' <Borders/>';
put ' <Font/>';
put ' <Interior/>';
put ' <NumberFormat/>';
put ' <Protection/>';
put ' </Style>';
put ' </Styles>';
/* Création du 1er onglet CSI */
put ' <Worksheet ss:Name="ENSEIGNE">';
put ' <Table ss:ExpandedColumnCount="256" ss:ExpandedRowCount="1" x:FullColumns="1"';
put ' x:FullRows="1" ss:DefaultColumnWidth="60">';
put ' <Column ss:AutoFitWidth="0" ss:Width="48" ss:Span="255"/>';
put ' </Table>';
put ' <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">';
put ' <PageSetup>';
put ' <PageMargins x:Bottom="0.984251969" x:Left="0.78740157499999996"';
put ' x:Right="0.78740157499999996" x:Top="0.984251969"/>';
put ' </PageSetup>';
put ' <Selected/>';
put ' <ProtectObjects>False</ProtectObjects>';
put ' <ProtectScenarios>False</ProtectScenarios>';
put ' </WorksheetOptions>';
put ' </Worksheet>';
/* Création du 2ième onglet CSI */
put ' <Worksheet ss:Name="CSI">';
put ' <Table ss:ExpandedColumnCount="256" ss:ExpandedRowCount="1" x:FullColumns="1"';
put ' x:FullRows="1" ss:DefaultColumnWidth="60">';
put ' <Column ss:AutoFitWidth="0" ss:Width="48" ss:Span="255"/>';
put ' </Table>';
put ' <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">';
put ' <PageSetup>';
put ' <PageMargins x:Bottom="0.984251969" x:Left="0.78740157499999996"';
put ' x:Right="0.78740157499999996" x:Top="0.984251969"/>';
put ' </PageSetup>';
put ' <ProtectObjects>False</ProtectObjects>';
put ' <ProtectScenarios>False</ProtectScenarios>';
put ' </WorksheetOptions>';
put ' </Worksheet>';
put '</Workbook>';
run; |
2° Exporter les données dans le fichier créer:
J'ai utilisé le macro programme suivant que j'ai récupéré dans une doc très bien faite et très utile d'Olivier Decourt, l'eport de SAS vers Excel expliqué à ma fille. Elle permet de récupérer automatiquement le nom des variables, leur format, etc. et de générer l'export vers Excel dans un classeur existant.
Code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| %MACRO sas2excel (tableSAS, classeurExcel, onglet=Feuil1) ;
FILENAME cible "&classeurExcel" ;
SYSTASK COMMAND "Start Excel" ;
DATA _NULL_ ;
CALL SLEEP(10,1) ;
RUN ;
FILENAME xlAppli DDE "Excel|system" ;
%IF %SYSFUNC(FEXIST(cible))=1 %THEN %DO ;
DATA _NULL_ ;
FILE xlAppli ;
PUT "[OPEN(""&classeurExcel"")]" ;
RUN ;
%END ;
DATA _NULL_ ;
FILE xlData ;
RUN ;
%IF &syserr >= 4 %THEN %DO ;
/* la feuille demandée n'existe pas */
%ABORT ;
%END ;
PROC CONTENTS DATA = &tableSAS
OUT = work.dictionnaire
NOPRINT ;
RUN ;
PROC SQL NOPRINT ;
SELECT COMPBL(name!!
CASE
WHEN (type=1 AND
format IS MISSING)
THEN " NUMX15.2 "
WHEN (type=1) THEN " "!!
COMPRESS(format!!
formatL!!"."!!
formatD)!!" "
ELSE ""
END),
QUOTE(LEFT(TRIM(COALESCE(label,name)))),
COUNT(*),
MAX(nobs)+1
INTO : VARIABLES SEPARATED BY " '09'x ",
: en_tete SEPARATED BY " '09'x ",
: nbVar ,
: nbObs
FROM work.dictionnaire
ORDER BY varnum
;
DROP TABLE work.dictionnaire ;
QUIT ;
FILENAME xlData DDE
"Excel|&onglet!L1C1:%SYSFUNC(COMPRESS(L&nbObs.C&nbVar))"
NOTAB ;
DATA _NULL_ ;
FILE xlData LRECL = 5000 ;
SET &tableSAS ;
IF _N_ = 1 THEN PUT &en_tete ;
PUT &VARIABLES ;
RUN ;
DATA _NULL_ ;
FILE xlAppli ;
PUT
%IF %SYSFUNC(FEXIST(cible))=1 %THEN %DO ;
"[SAVE()]"
%END ;
%ELSE %DO ;
"[SAVE.AS(""&classeurExcel"")]"
%END ;
;
PUT "[QUIT()]" ;
RUN ;
%MEND sas2excel ; |
Tous ça est bien sûr paramétrable pour créer plusieurs fichiers avec différents onglets et d'y exporter des données.