Hello all,
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!
Use this
call symput('timeofday',trim(timeofday));
when creating the macro variable.
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.
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
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"
.....
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;
Okay I'll try playing with it using these suggestions and let you know how it goes. Thanks everyone
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.
You're very close. Take a closer look at Reeza's suggestion, and add the missing "X": call symputx
call symputX('timeofday',timeofday);
Use this
call symput('timeofday',trim(timeofday));
when creating the macro variable.
Got it to work with this, thank you again everybody!
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.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.