I was hoping for help/ an explanation understanding why in the demo video that in the where statement the first macro is not in in double quotation marks but the other two are? I was trying to look up if the following two were string literals and maybe that had something to do with that and I searched the course and reviewed the syntax video but I'm still confused. I'll post an image of the video and I'm happy to go back and review lectures again, I just think I need some clarification.
@ivarenho wrote:
The date from June 1980, please correct me if I'm wrong but that has to be in the quotation marks because it's making a calculation?
Certain types of calculations can be done in SAS without quotation marks. It depends on whether or not quotation marks are required to create valid SAS code. In some cases, quotation marks are invalid at that point in the SAS code and will cause SAS to generate errors.
This is why @D_Dunlap said you really should first generate working SAS code without macros variables, and hard-code the values that the macro variable will take — and if it works without quotes, then you don't need to quotes when you use the macro variable. If it doesn't work unless you put the quotes in, then you need the quotes when you use a macro variable. This is why @Tom said "The code that results once the macro processor is finished needs to be actual valid SAS code." So please, do yourself a favor and first create working (emphasis on "working") code without macro variables and hard coded values where the macro variables will go. Then using the macro variables correctly will be a much easier task.
Hello @ivarenho ,
The substitutions that are made by macro variables need to match the type of the variable in the data set. The MaxWindMPH variable is a numeric variable, the Basin variable is a character variable, and StartDate is a numeric date value (number of days since Jan. 1, 1960).
Remember, you should always start with hard coded values before you start making macro variable substitutions. So if the original code is below. What do you notice? The value for MaxWindMPH is numeric token, so it does not have quotes around it. Basin is a character token, so it has double quotes around it and double quotes are required when you need a substitution for a character token. StartDate should be a numeric SAS date value, so the date constant (d) converts the values in double quotes to a numeric value that represents the number of days since Jan. 1, 1960.
proc print data=pg1.storm_summary; where MaxWindMPH>100 and Basin="EP" and StartDate>="01Jun1980"d; var Basin Name StartDate EndDate MaxWindMPH; run;
In the code below, notice I used the SYMBOLGEN option to display the resolved values of the macro variables when the substitutions are made:
20 options symbolgen;
21
22 %let WindSpeed=100;
23 %let BasinCode=EP;
24 %let Date=01Jun1980;
25
26 proc print data=pg1.storm_summary;
27 where MaxWindMPH>&WindSpeed and Basin="&BasinCode" and StartDate>="&Date"d;
SYMBOLGEN: Macro variable WINDSPEED resolves to 100
SYMBOLGEN: Macro variable BASINCODE resolves to EP
SYMBOLGEN: Macro variable DATE resolves to 01Jun1980
28 var Basin Name StartDate EndDate MaxWindMPH;
29 run;
NOTE: There were 208 observations read from the data set PG1.STORM_SUMMARY.
WHERE (MaxWindMPH>100) and (Basin='EP') and (StartDate>='01JUN1980'D);
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.03 seconds
cpu time 0.01 seconds
I hope this helps.
The rules of the macro values following the token type makes sense. This is the part that I was missing. I was trying to figure out the type of data or the term you correctly pointed out to me is token type. The date from June 1980, please correct me if I'm wrong but that has to be in the quotation marks because it's making a calculation?
To make a DATE literal value you need text in a style that the DATE informat can understand enclosed in quotes and suffixed with the letter D.
Examples:
'01JAN1980'd
"1jan80"d
"01-jan-1980"D
You could also just supply the actual number of days since start of 1960.
7305
But that is a lot harder for humans to read or enter.
You don't normally need to think about tokens or how SAS parses the code. Instead you just need to remember that the macro processor is just a normal text pre-processor. The code that results once the macro processor is finished needs to be actual valid SAS code.
@ivarenho wrote:
The date from June 1980, please correct me if I'm wrong but that has to be in the quotation marks because it's making a calculation?
Certain types of calculations can be done in SAS without quotation marks. It depends on whether or not quotation marks are required to create valid SAS code. In some cases, quotation marks are invalid at that point in the SAS code and will cause SAS to generate errors.
This is why @D_Dunlap said you really should first generate working SAS code without macros variables, and hard-code the values that the macro variable will take — and if it works without quotes, then you don't need to quotes when you use the macro variable. If it doesn't work unless you put the quotes in, then you need the quotes when you use a macro variable. This is why @Tom said "The code that results once the macro processor is finished needs to be actual valid SAS code." So please, do yourself a favor and first create working (emphasis on "working") code without macro variables and hard coded values where the macro variables will go. Then using the macro variables correctly will be a much easier task.
I often will re-word what someone says to make sure I understand, I appreciate you explaining what was said by @D_Dunlap , what you and they are both say is to make sure the code works before using it to create the marco variables. That makes sense to me. This way, if there is an error with the code before creating the macro, I can correct that error before creating the macro variable.
This is great to hear @ivarenho . So many visitors only want the answer to their programming problem and learn nothing useful so next time they have similar problems. So many visitors to this forum receive the same advice about creating a working program without macro variables first, and they ignore the advice and move on to struggle mightily with macros.
Dive into keynotes, announcements and breakthroughs on demand.
Explore Now →SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.