Christoffer
Christoffer

Reputation: 346

SAS EG export to different test folder conditional statement

My code exports a table from SAS to a excel file in a folder. My goal to change the export folder to a test folder if I set the variable test to 1. This is my code:

/*Delete .bak files*/
%macro xlsx_bak_delete(file, file_name) / des='Delete backup spreadsheets';
option mprint notes;
data _null_;
fname = 'todelete';
rc = filename(fname, "&file.&file_name..xlsx.bak");
rc = fdelete(fname);
rc = filename(fname);
run;
%mend xlsx_bak_delete;

/*Export*/
%macro ExportExcel(path,file_name,table_name);
proc export data=&table_name
    outfile="&path.&file_name..xlsx"
    dbms=xlsx
    replace;
    ;
run;
%xlsx_bak_delete(&export_path, &file_name)
%mend;

%LET test = 1

%IF test = 1 %then %do;
    /*TEST export path */
    %Let export_path = \\Bfd1\b00369\Afdeling\HS-OKO\Oko\Likviditet\Likviditetsstyring\LCR\Daglig LCR - Axiom\Test\;
%end;
%else %do;
    /*Export path*/
    %Let export_path = \\Bfd1\b00369\Afdeling\HS-OKO\Oko\Likviditet\Likviditetsstyring\LCR\Daglig LCR - Axiom\Test_prod\;
%end;

/*Datetiemstamp macro*/
%let fileTimeStamp = %sysfunc(date(), ddmmyyd10.)_%sysfunc(putc(%sysfunc(time(), b8601TM6.), $6.)) ;
%put &fileTimeStamp.;


%ExportExcel(&export_path,DAGLIGEKORREKTIONER_&fileTimeStamp.,QUERY_FOR_DAGLIGEKORREKTIONER);

The issue is that my code only exports to the test_prod folder nothing changes if test is = 0 or 1 why is that?

Upvotes: 0

Views: 116

Answers (1)

Kermit
Kermit

Reputation: 3117

You are incorrectly referencing the macro variable. As a result, it will always end up in the %else %do; part of the %if statement.
You can use the variable by referencing it with an ampersand preceding its name (&test in your case), which is called a macro variable reference.

You are also missing a semicolon ; after the %LET statement.

%LET test = 1;

%IF &test = 1 %then %do;
    /*TEST export path */
    %Let export_path = \\Bfd1\b00369\Afdeling\HS-OKO\Oko\Likviditet\Likviditetsstyring\LCR\Daglig LCR - Axiom\Test\;
%end;
%else %do;
    /*Export path*/
    %Let export_path = \\Bfd1\b00369\Afdeling\HS-OKO\Oko\Likviditet\Likviditetsstyring\LCR\Daglig LCR - Axiom\Test_prod\;
%end;

Upvotes: 1

Related Questions