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

Hi, 

Please, how should I have written this code to fix these problems?

I I use this option A the code doesn't export  the TXT file , If I use OPTION B then it exports however I received a error message 

" The if statement is not valid in open code" and " The end statement is not valid in open code"

I thought that option A could be right to avoid this problem however using this option is doesn't work , I mean the txt file is not  created.

My SAS version is 9.04.01M4P110916

 

 
/* OPTION A - PROC EXPORT -*/
 %macro test;
%if (&export_flag = 'Y') %then %do;
 
 PROC EXPORT DATA=JORQUEC.TRIGGERPAYG
 OUTFILE='//SASCommon/jorquec/TRIGGERPAYG.TXT'
 DBMS=dlm 
 REPLACE;
  delimiter='&';
 RUN;
 
%end;
 %mend;
 %test;

 

 

OPTION B 

/* OPTION B- PROC EXPORT -*/
 
%if (&export_flag = 'Y') %then %do;
 
 PROC EXPORT DATA=JORQUEC.TRIGGERPAYG
 OUTFILE='//SASCommon/jorquec/TRIGGERPAYG.TXT'
 DBMS=dlm 
 REPLACE;
  delimiter='&';
 RUN;
 
%end;
 
1 ACCEPTED SOLUTION

Accepted Solutions
Criptic
Lapis Lazuli | Level 10

%if, %else, etc. only work in open code if you have SAS9.4M5 or up (see https://blogs.sas.com/content/sasdummy/2018/07/05/if-then-else-sas-programs/).

 

Try putting your code inside of macro and calling it.

View solution in original post

7 REPLIES 7
Kurt_Bremser
Super User

Since SAS 9.4, %if %then %do; %end; can be used in open code.

But I guess your problem comes from the fact that you try to deal with values in macros the way one does with data step values.

The macro preprocessor is a TEXT engine, knowing only this datatype, so quotes are almost never needed.

So if you do

call symputx('export_flag','Y');

then your code should be

%if &export_flag = Y %then %do;

proc export
  data=JORQUEC.TRIGGERPAYG
  outfile='//SASCommon/jorquec/TRIGGERPAYG.TXT'
  dbms=dlm 
  replace
;
delimiter='&';
run;
 
%end;
jorquec
Quartz | Level 8
Hi 
I changed as you suggested, now it works , the export file is created however I still receive this message :
ERROR: The %END statement is not valid in open code.
 
 
 
/* STEP 2 */
 
DATA JORQUEC.TRIGGERPAYG;
SET JORQUEC.TESTPAYG;
IF  (maxmargin_dt >= (&ONEDT2.)/100) and (maxmodelmart_d = &month_id.) then export_flag='Y'; 
else export_flag='N';
 
call symputx('export_flag', export_flag);
 
run;
 
*check value of macro variable;
%PUT Export_Flag = &export_flag;
 
/* STEP 3 - PROC EXPORT -*/
 
%if &export_flag = Y%then %do;
 
 PROC EXPORT DATA=JORQUEC.TRIGGERPAYG
 OUTFILE='//SASCommon/jorquec/TRIGGERPAYG.TXT'
 DBMS=dlm 
 REPLACE;
  delimiter='&';
 RUN;
 
%end;
 
jorquec
Quartz | Level 8
1                                                          The SAS System                             08:22 Wednesday, July 24, 2019

1          ;*';*";*/;quit;run;
2          OPTIONS PAGENO=MIN;
3          %LET _CLIENTTASKLABEL='FINAL_VERSION';
4          %LET _CLIENTPROCESSFLOWNAME='Process Flow';
5          %LET _CLIENTPROJECTPATH='D:\SAS\jorquec\Model_rebuild\2019\Manangement Console\Trigger event_PAYG.egp';
6          %LET _CLIENTPROJECTPATHHOST='RESCZKWTS004';
7          %LET _CLIENTPROJECTNAME='Trigger event_PAYG.egp';
8          %LET _SASPROGRAMFILE='';
9          %LET _SASPROGRAMFILEHOST='';
10         
11         ODS _ALL_ CLOSE;
12         OPTIONS DEV=PNG;
13         GOPTIONS XPIXELS=0 YPIXELS=0;
14         FILENAME EGPDF TEMP;
15         ODS PDF(ID=EGPDF) FILE=EGPDF STYLE=Pearl;
NOTE: Writing ODS PDF(EGPDF) output to DISK destination "EGPDF", printer "PDF".
16         FILENAME EGSR TEMP;
17         ODS tagsets.sasreport13(ID=EGSR) FILE=EGSR
18             STYLE=HtmlBlue
19             STYLESHEET=(URL="file:///C:/SAS94/software/SASEnterpriseGuide/7.1/Styles/HtmlBlue.css")
20             NOGTITLE
21             NOGFOOTNOTE
22             GPATH=&sasworklocation
23             ENCODING=UTF8
24             options(rolap="on")
25         ;
NOTE: Writing TAGSETS.SASREPORT13(EGSR) Body file: EGSR
26         FILENAME EGXLSSX TEMP;
27         ODS EXCEL(ID=EGXLSSX) FILE=EGXLSSX STYLE=Excel
28         OPTIONS (
29          EMBEDDED_TITLES="no" EMBEDDED_FOOTNOTES="no"
30         );
31         
32         GOPTIONS ACCESSIBLE;
ERROR: The %IF statement is not valid in open code.
33         %if &export_flag = Y %then %do;
34         
35         data jorquec.TRIGGERPAYG2;
36         SET JORQUEC.TRIGGERPAYG;
37         run;

NOTE: There were 1 observations read from the data set JORQUEC.TRIGGERPAYG.
NOTE: The data set JORQUEC.TRIGGERPAYG2 has 1 observations and 4 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      user cpu time       0.00 seconds
      system cpu time     0.00 seconds
      memory              564.93k
      OS Memory           38604.00k
      Timestamp           24/07/2019 10:25:52 AM
      Step Count                        15  Switch Count  2
      Page Faults                       0
      Page Reclaims                     59
      Page Swaps                        0
      Voluntary Context Switches        12
      Involuntary Context Switches      1
      Block Input Operations            0
      Block Output Operations           136
2                                                          The SAS System                             08:22 Wednesday, July 24, 2019

      

38         
39         /* PROC EXPORT DATA=JORQUEC.TRIGGERPAYG
40          OUTFILE='//SASCommon/jorquec/TRIGGERPAYG.TXT'
41          DBMS=dlm
42          REPLACE;
43           delimiter='&';
44          RUN;*/
45         
46         %end;
ERROR: The %END statement is not valid in open code.
47         
48         GOPTIONS NOACCESSIBLE;
49         %LET _CLIENTTASKLABEL=;
50         %LET _CLIENTPROCESSFLOWNAME=;
51         %LET _CLIENTPROJECTPATH=;
52         %LET _CLIENTPROJECTPATHHOST=;
53         %LET _CLIENTPROJECTNAME=;
54         %LET _SASPROGRAMFILE=;
55         %LET _SASPROGRAMFILEHOST=;
56         
57         ;*';*";*/;quit;run;
58         ODS _ALL_ CLOSE;
NOTE: ODS PDF(EGPDF) printed no output. 
      (This sometimes results from failing to place a RUN statement before the ODS PDF(EGPDF) CLOSE statement.)
NOTE: Writing EXCEL(EGXLSSX) file: /saswork94/SAS_work467F000089F0_czhrk-dl580nucprod-310-fe/#LN01122
59         
60         
61         QUIT; RUN;
Kurt_Bremser
Super User

You should pressure your SAS admin(s) to upgrade to the current maintenance level. Especially M5 had a lot of goodies. And with the current package, you get EG 8.1.

Criptic
Lapis Lazuli | Level 10

%if, %else, etc. only work in open code if you have SAS9.4M5 or up (see https://blogs.sas.com/content/sasdummy/2018/07/05/if-then-else-sas-programs/).

 

Try putting your code inside of macro and calling it.

jorquec
Quartz | Level 8
 
 %macro test;
%if &export_flag = Y %then %do;

data jorquec.TRIGGERPAYG;
SET JORQUEC.TESTPAYG2;
run;

%end;
 %mend;
 %test;

/* Many thanks, now is fine!! */