BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
aamoen
Obsidian | Level 7

I'm getting this error from the following code: ERROR 180-322: Statement is not valid or it is used out of proper order.

 

%macro append_months(start=202201, end=202504);

%local y m ym setlist;
%let setlist=;

%do y = %substr(&start, 1, 4) %to %substr(&end, 1, 4);
    %do m = 1 %to 12;
        %let ym = %sysfunc(putn(&y, z4.))%sysfunc(putn(&m, z2.));
        %if %eval(&ym >= &start and &ym <= &end) %then %do;
            %let setlist = &setlist perfout.acct_lvl_&ym(drop=MTHOPN);
        %end;
    %end;
%end;

data perf.gpcc_perf_append;
    set &setlist;
    keep account_id YYYYMM MthSncAction EXSTATCD EXSTATRSNCD d_apply_mth d_apply_qtr d_book_mth d_book_qtr c_portfolio
         AVGBAL DLQ000 DLQ000BAL DLQ001 DLQ001BAL DLQ030 DLQ030BAL DLQ060 DLQ060BAL DLQ060X DLQ060XBAL
         DLQ090 DLQ090BAL DLQ090X DLQ090XBAL DLQ120 DLQ120BAL DLQ150 DLQ150BAL DLQ180 DLQ180BAL
         BALEOM COPRNBAL RECOVERYPRNAMT REVTOTNET CO COBK COCUM;
run;

%mend append_months;

%append_months(start=202201, end=202504);

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

All you need:

data _null_;
length setlist $32000;
date = input("202201",yymmn6.);
do while (date le input("202504",yymmn6.));
  setlist = catx(" ",setlist,"perfout.acct_lvl_" !! put(date,yymmn6.) !! "(drop=MTHOPN)");
  date = intnx('month`,date,1,"b");
end;
call symputx("setlist",setlist);
run;

data perf.gpcc_perf_append;
    set &setlist;
    keep account_id YYYYMM MthSncAction EXSTATCD EXSTATRSNCD d_apply_mth d_apply_qtr d_book_mth d_book_qtr c_portfolio
         AVGBAL DLQ000 DLQ000BAL DLQ001 DLQ001BAL DLQ030 DLQ030BAL DLQ060 DLQ060BAL DLQ060X DLQ060XBAL
         DLQ090 DLQ090BAL DLQ090X DLQ090XBAL DLQ120 DLQ120BAL DLQ150 DLQ150BAL DLQ180 DLQ180BAL
         BALEOM COPRNBAL RECOVERYPRNAMT REVTOTNET CO COBK COCUM;
run;

No macro loop needed, and it's much easier in a DATA step.

Also see the advantage of using SAS date values, as it allows the use of the date functions.

 

PS untested code, posted from my tablet.

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Since you are getting errors in a macro program, please turn on macro debugging by running this command

 

options mprint;

 

then run the macro program again, and show us the LOG. We need to see the ENTIRE log (not selected parts, not just the error messages) from this macro.

 

Please copy the log as text and paste it into the window that appears when you click on the </> icon. DO NOT SKIP THIS STEP.

PaigeMiller_0-1715196634946.png

 

Also please explain in words (not code) what this macro is trying to do.

--
Paige Miller
aamoen
Obsidian | Level 7


1 ;*';*";*/;quit;run;
2 OPTIONS PAGENO=MIN;
3 %LET _CLIENTTASKLABEL='Perf_Metrics.sas';
4 %LET _CLIENTPROCESSFLOWNAME='Standalone Not In Project';
5 %LET _CLIENTPROJECTPATH='';
6 %LET _CLIENTPROJECTPATHHOST='';
7 %LET _CLIENTPROJECTNAME='';
8 %LET _SASPROGRAMFILE='C:\Perf_Metrics.sas';

9 %LET _SASPROGRAMFILEHOST=;
10
11 ODS _ALL_ CLOSE;
12 OPTIONS DEV=SVG;

13 GOPTIONS XPIXELS=0 YPIXELS=0;mer_
14 %macro HTML5AccessibleGraphSupported;
15 %if %_SAS_VERCOMP_FV(9,4,4, 0,0,0) >= 0 %then ACCESSIBLE_GRAPH;
16 %mend;
17 FILENAME EGHTML TEMP;
18 ODS HTML5(ID=EGHTML) FILE=EGHTML
19 OPTIONS(BITMAP_MODE='INLINE')
20 %HTML5AccessibleGraphSupported
21 ENCODING='utf-8'
22 STYLE=HTMLBlue
23 NOGTITLE
24 NOGFOOTNOTE
25 GPATH=&sasworklocation
26 ;
NOTE: Writing HTML5(EGHTML) Body file: EGHTML
27
28
29 options mprint;
30 %macro append_months(start=202201, end=202504);
31
32 %local y m ym setlist;
33 %let setlist=;
34
35 %do y = %substr(&start, 1, 4) %to %substr(&end, 1, 4);
36     %do m = 1 %to 12;
37         %let ym = %sysfunc(putn(&y, z4.))%sysfunc(putn(&m, z2.));
38         %if %eval(&ym >= &start and &ym <= &end) %then %do;
39             %let setlist = &setlist perfout.acct_lvl_&ym(drop=MTHOPN);
40         %end;
41     %end;
42 %end;
43
44 data perf.gpcc_perf_append;
45     set &setlist;
46     keep account_id YYYYMM MthSncAction EXSTATCD EXSTATRSNCD d_apply_mth d_apply_qtr d_book_mth d_book_qtr c_portfolio
47          AVGBAL DLQ000 DLQ000BAL DLQ001 DLQ001BAL DLQ030 DLQ030BAL DLQ060 DLQ060BAL DLQ060X DLQ060XBAL
48          DLQ090 DLQ090BAL DLQ090X DLQ090XBAL DLQ120 DLQ120BAL DLQ150 DLQ150BAL DLQ180 DLQ180BAL
49          BALEOM COPRNBAL RECOVERYPRNAMT REVTOTNET CO COBK COCUM;
50 run;
51
52 %mend append_months;
53
54 %append_months(start=202201, end=202504);
NOTE: Line generated by the invoked macro "APPEND_MONTHS".
54    
___
2 The SAS System 13:27 Wednesday, July 2, 2025

180
ERROR 180-322: Statement is not valid or it is used out of proper order.

MPRINT(APPEND_MONTHS):                                                                                                    
                                                                                                                                   
                                                                                                                                   
                                                                                                                                   
                                                                                                                               
                                                                                                                                   
                                                                                                                                   
                                                                                                                                   
                                                                                                                               
                                                                                                                                   
                                                                                                                                   
                                                                                                                                   
                                                                                                                                   
                                                                                                    data perf.gpcc_perf_append;

NOTE: Line generated by the invoked macro "APPEND_MONTHS".
54     set &setlist;     keep account_id YYYYMM MthSncAction EXSTATCD EXSTATRSNCD d_apply_mth
___
180
54 ! d_apply_qtr d_book_mth d_book_qtr c_portfolio          AVGBAL DLQ000 DLQ000BAL DLQ001 DLQ001BAL DLQ030 DLQ030BAL DLQ060
54 ! DLQ060BAL
MPRINT(APPEND_MONTHS):     set perfout.acct_lvl_202201(drop=MTHOPN) perfout.acct_lvl_202202(drop=MTHOPN)
perfout.acct_lvl_202203(drop=MTHOPN) perfout.acct_lvl_202204(drop=MTHOPN) perfout.acct_lvl_202205(drop=MTHOPN)
perfout.acct_lvl_202206(drop=MTHOPN) perfout.acct_lvl_202207(drop=MTHOPN) perfout.acct_lvl_202208(drop=MTHOPN)
perfout.acct_lvl_202209(drop=MTHOPN) perfout.acct_lvl_202210(drop=MTHOPN) perfout.acct_lvl_202211(drop=MTHOPN)
perfout.acct_lvl_202212(drop=MTHOPN) perfout.acct_lvl_202301(drop=MTHOPN) perfout.acct_lvl_202302(drop=MTHOPN)
perfout.acct_lvl_202303(drop=MTHOPN) perfout.acct_lvl_202304(drop=MTHOPN) perfout.acct_lvl_202305(drop=MTHOPN)
perfout.acct_lvl_202306(drop=MTHOPN) perfout.acct_lvl_202307(drop=MTHOPN) perfout.acct_lvl_202308(drop=MTHOPN)
perfout.acct_lvl_202309(drop=MTHOPN) perfout.acct_lvl_202310(drop=MTHOPN) perfout.acct_lvl_202311(drop=MTHOPN)
perfout.acct_lvl_202312(drop=MTHOPN) perfout.acct_lvl_202401(drop=MTHOPN) perfout.acct_lvl_202402(drop=MTHOPN)
perfout.acct_lvl_202403(drop=MTHOPN) perfout.acct_lvl_202404(drop=MTHOPN) perfout.acct_lvl_202405(drop=MTHOPN)
perfout.acct_lvl_202406(drop=MTHOPN) perfout.acct_lvl_202407(drop=MTHOPN) perfout.acct_lvl_202408(drop=MTHOPN)
perfout.acct_lvl_202409(drop=MTHOPN) perfout.acct_lvl_202410(drop=MTHOPN) perfout.acct_lvl_202411(drop=MTHOPN)
perfout.acct_lvl_202412(drop=MTHOPN) perfout.acct_lvl_202501(drop=MTHOPN) perfout.acct_lvl_202502(drop=MTHOPN)
perfout.acct_lvl_202503(drop=MTHOPN) perfout.acct_lvl_202504(drop=MTHOPN);

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the invoked macro "APPEND_MONTHS".
54 data perf.gpcc_perf_append;     set &setlist;     keep account_id YYYYMM MthSncAction EXSTATCD EXSTATRSNCD d_apply_mth
___
180
54 ! d_apply_qtr d_book_mth d_book_qtr c_portfolio          AVGBAL DLQ000 DLQ000BAL DLQ001 DLQ001BAL DLQ030 DLQ030BAL DLQ060
54 ! DLQ060BAL
ERROR 180-322: Statement is not valid or it is used out of proper order.

MPRINT(APPEND_MONTHS):     keep account_id YYYYMM MthSncAction EXSTATCD EXSTATRSNCD d_apply_mth d_apply_qtr d_book_mth d_book_qtr
c_portfolio          AVGBAL DLQ000 DLQ000BAL DLQ001 DLQ001BAL DLQ030 DLQ030BAL DLQ060 DLQ060BAL DLQ060X DLQ060XBAL          DLQ090
DLQ090BAL DLQ090X DLQ090XBAL DLQ120 DLQ120BAL DLQ150 DLQ150BAL DLQ180 DLQ180BAL          BALEOM COPRNBAL RECOVERYPRNAMT REVTOTNET
CO COBK COCUM;

MPRINT(APPEND_MONTHS): run;
55
56
57 %LET _CLIENTTASKLABEL=;
3 The SAS System 13:27 Wednesday, July 2, 2025

58 %LET _CLIENTPROCESSFLOWNAME=;
59 %LET _CLIENTPROJECTPATH=;
60 %LET _CLIENTPROJECTPATHHOST=;
61 %LET _CLIENTPROJECTNAME=;
62 %LET _SASPROGRAMFILE=;
63 %LET _SASPROGRAMFILEHOST=;
64
65 ;*';*";*/;quit;run;
66 ODS _ALL_ CLOSE;
67
68
69 QUIT; RUN;
70

Tom
Super User Tom
Super User

Looks like you might have some invisible characters in the file.

 

Note that pasting the log without using the pop-up windows generated by the Insert Code or Insert SAS Code buttons causes the spacing to get messed up.  That makes if very hard to tell exactly which character in the LOG SAS is trying to mark as the location where it detected the syntax error.

Quentin
Super User

Looks like you might be able to get what you want with :

 

data perf.gpcc_perf_append;
    set perfout.acct_lvl_202201-perfout.acct_lvl_202504 ;
    * ... ;
run;

 

The Boston Area SAS Users Group is hosting free webinars!
Next up: Lisa Mendez & Richann Watson present Get Tipsy with Debugging Tips for SAS® Code: The After Party on Wednesday Jul 16.
Register now at https://www.basug.org/events.
PaigeMiller
Diamond | Level 26

Looks like you have several good answers. However, it is also very helpful to us (and therefore helpful to you) if you explain what the macro is supposed to do, like I asked. Please keep that in mind in your future questions.

--
Paige Miller
Kurt_Bremser
Super User

All you need:

data _null_;
length setlist $32000;
date = input("202201",yymmn6.);
do while (date le input("202504",yymmn6.));
  setlist = catx(" ",setlist,"perfout.acct_lvl_" !! put(date,yymmn6.) !! "(drop=MTHOPN)");
  date = intnx('month`,date,1,"b");
end;
call symputx("setlist",setlist);
run;

data perf.gpcc_perf_append;
    set &setlist;
    keep account_id YYYYMM MthSncAction EXSTATCD EXSTATRSNCD d_apply_mth d_apply_qtr d_book_mth d_book_qtr c_portfolio
         AVGBAL DLQ000 DLQ000BAL DLQ001 DLQ001BAL DLQ030 DLQ030BAL DLQ060 DLQ060BAL DLQ060X DLQ060XBAL
         DLQ090 DLQ090BAL DLQ090X DLQ090XBAL DLQ120 DLQ120BAL DLQ150 DLQ150BAL DLQ180 DLQ180BAL
         BALEOM COPRNBAL RECOVERYPRNAMT REVTOTNET CO COBK COCUM;
run;

No macro loop needed, and it's much easier in a DATA step.

Also see the advantage of using SAS date values, as it allows the use of the date functions.

 

PS untested code, posted from my tablet.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 649 views
  • 0 likes
  • 5 in conversation