03-17-2017 01:53 PM
Currently I'm writing a SAS program that will send out a status email and depending on the time of day I want it to change the Subject line of the email to say Noon/Afternoon/Evening status email. Below is the IF statement I use to decide what the subject line should read, however I am getting a problem. Since 'Noon' is the first to be looked at it makes the variable 'timeofday' set to 4 character bytes, so when it's actually 1600 hours and selects 'Afternoon' the code actually spits out 'Afte' into the subject line. In other words because timeofday shows up first as being set to 'Noon' it is restricting that variable to being set with only 4 characters.
IF hour(datetime()) = 12 THEN timeofday = "Noon";
ELSE IF hour(datetime()) = 16 THEN timeofday = "Afternoon";
ELSE IF hour(datetime()) = 19 THEN timeofday = "Evening";
ELSE timeofday = "NA";
I tried using a length statement to make it allow at least 10 character bytes into the variable, however when I try this it fills in the empty spaces with spaces making the subject line look weird. For example for noon it spits out the following 'Noon '. I tried using the below bit of code to set this part.
length timeofday $10 default=4;
Any tips would be greatly appreciated!
03-17-2017 02:44 PM
03-17-2017 01:57 PM
Are the trailing spaces causing an issue somehow?
Unfortunately that's the behaviour of SAS character variables and I don't know a way to work around it in a data set.
For reports or other calculations you can use either COMPRESS, TRIM, CATX() to remove the trailing spaces.
03-17-2017 02:01 PM
Show the statement you are using, in your code, to create the email. If it's a put statement, or something similar, I'm guessing that you could wrap it in a trim() or strip() function to get rid of the undesired spaces.
Art, CEO, AnalystFinder.com
03-17-2017 02:07 PM
This is how the timeofday variable ends up getting used. Sorry I would post the whole code but I don't want to post anything I shouldn't.
subject = "ATTN: Daily &timeofday Status for Jobs"
03-17-2017 02:12 PM
The issue is then when you create the macro variable, NOT the length of the variable.
If using a data step then use CALL SYMPUTX().
If SQL use TRIMMED option.
Otherwise do something like the following which is a quick way to remove the spaces. I prefer one of the methods above since it's explicit and clear as to why you're doing this.
%let trimmed_var = &old_var_name;
03-17-2017 02:33 PM - edited 03-17-2017 02:39 PM
So I tried using a CALL symput('timeofday', timeofday); in the code both before the if statement, after the if statement, and in a seperate data step but none of these scenarios had any affect.
Also I don't use any SQL so the trim won't be an option in this case.
03-17-2017 02:44 PM
03-17-2017 02:16 PM
In your first post you created the datastep variable, but here you use a macro variable. Without knowing how one is turned into the other, it's hard to help you.
Need further help from the community? Please ask a new question.