Search code examples
sassas-macro

'Hello World' in SAS with a macro gives syntax error


I am extremely new to the SAS world so naturally I was trying to write my 'Hello World'. Here is my MWE which gives me the syntax errors:

/* SAS Hello World Program */

    /*Macro with date*/
    %Macro datum;
    Title "Hello World, today is &Sysday, &Sysdate";
    %Mend datum;

    /*Create Hello World Data Set */     
    data HelloWorld;
    msg = %datum ;
    run;

    /*Print Hello World*/
    proc print data = HelloWorld;
    run;

It does not print the 'Hello Wolrd'-message instead it gives a syntax error which I don't understand. In the Log the message appears, so in principle it works - just the print step doesn't. Any ideas?


Solution

  • The macro you created generates a TITLE statement. So it should work fine.

    But your program is using it in the wrong place. Once your macro runs and finishes generating the text of the TITLE statement your data step will look like this:

    data HelloWorld;
      msg = Title "Hello World, today is Thursday, 10JAN19"; ;
    run;
    

    Which will obviously give an error since the right side of the assignment statement now has two tokens, a variable named TITLE and a string constant, without any operator between them. The extra semi-colon will just generate an extra null statement and not cause any problems.

    Perhaps you want to create a macro VARIABLE instead of an actual macro?

    To do that your program would look more like this.

    %let msg=Hello World, today is &Sysday, &Sysdate ;
    
    data HelloWorld;
      msg = "&msg." ;
    run;
    

    So when the macro variable reference is replaced this will evaluate to this SAS code to run.

    data HelloWorld;
      msg = "Hello World, today is Thursday, 10JAN19" ;
    run;
    

    Notice how you use & to trigger the evaluation of a macro variable. Also notice how I did not add the quotes to the value of the macro variable, but included them in the SAS code that was generated by using the value of macro variable.

    NOTE that it has yesterday's day of the week and date. That is because the automatic macro variables SYSDAY and SYSDATE are set when SAS starts running and I ran this code in a SAS session that I started yesterday.