Joakim M. Viland
Joakim M. Viland

Reputation: 23

Prompt: Date, %Sysfunc(year(&myvar.));

I have a SAS EG 7.1 program that generates reports on a monthly basis. The old process was very manual, where users, amongst other things, were asked to update a macro variable (&date.) with current month-end (Date9.).

    %let Date = '28feb2018'd; /* <--- Input the date to be reported  */

    /* Generate the year and the month based on the input date */
    %let year = %sysfunc(year(&date.)); 
    %let month = %sysfunc(month(&date.));

The &year (4-digit) and &month. (1- or 2-digit depending on the month) are later used in the code as components of a filename to pick up current month's tables:

    FROM lib.great_table_loc_&year._&month.;

I am trying to make the program dynamic by prompting the user to chose the current month-end date. The prompt is working, and outputs date9. format.

I then tried using the macro variable &Prompt_date. in the %sysfunc:

    %let Date = '&prompt_date.'; 
    %let year = %sysfunc(year(&prompt_date.)); 
    %let month = %sysfunc(month(&prompt_date.));

When I execute, I get the following error messages:

1)

    35         %let year = %sysfunc(year(&prompt_date.));
    ERROR: Argument 1 to function YEAR referenced by the %SYSFUNC or 
    %QSYSFUNC macro function is not a number.
    ERROR: Invalid arguments detected in %SYSCALL, %SYSFUNC, or %QSYSFUNC 
    argument list.  Execution of %SYSCALL statement or %SYSFUNC 
           or %QSYSFUNC function reference is terminated.
    36         %let month = %sysfunc(month(&prompt_date.));
    ERROR: Argument 1 to function MONTH referenced by the %SYSFUNC or 
    %QSYSFUNC macro function is not a number.
    ERROR: Invalid arguments detected in %SYSCALL, %SYSFUNC, or %QSYSFUNC 
    argument list.  Execution of %SYSCALL statement or %SYSFUNC 
           or %QSYSFUNC function reference is terminated. 

2)

    118         lib.great_table_loc_._.
                                    _
                                    22
                                    200
    ERROR 22-322: Syntax error, expecting one of the following: a name, ;, 
    (, ',', ANSIMISS, AS, CROSS, EXCEPT, FULL, GROUP, HAVING, 
          INNER, INTERSECT, JOIN, LEFT, NATURAL, NOMISS, ORDER, OUTER, 
    RIGHT, UNION, WHERE.  

    ERROR 200-322: The symbol is not recognized and will be ignored.

I've tried creating a separate field for month and year, formatted to YYMMN6., input the &prompt_date. and then put(,n.) to make the value numeric - still nothing.

Has anyone encountered a similar problem? Any tips on how to get what I want?

Thanks!

Upvotes: 2

Views: 1729

Answers (1)

DomPazz
DomPazz

Reputation: 12465

You have:

%let Date = '&prompt_date.'; 
%let year = %sysfunc(year(&prompt_date.)); 
%let month = %sysfunc(month(&prompt_date.));

Two issues.

  1. you assign &Date but don't use it.
  2. prompt date is DDMONYYYY (date9.). SAS will see that as a string. You need quotes and a d to make it a date.

Try

%let year = %sysfunc(year("&prompt_date."d)); 
%let month = %sysfunc(month("&prompt_date."d));

Upvotes: 3

Related Questions