BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cmacknair
Calcite | Level 5

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!

1 ACCEPTED SOLUTION
11 REPLIES 11
Reeza
Super User

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.

art297
Opal | Level 21

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

 

cmacknair
Calcite | Level 5

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"

.....

Reeza
Super User

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;

cmacknair
Calcite | Level 5

Okay I'll try playing with it using these suggestions and let you know how it goes. Thanks everyone

cmacknair
Calcite | Level 5

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.

Astounding
PROC Star

You're very close.  Take a closer look at Reeza's suggestion, and add the missing "X":  call symputx

Reeza
Super User
call symputX('timeofday',timeofday);
cmacknair
Calcite | Level 5

Got it to work with this, thank you again everybody!

Kurt_Bremser
Super User

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.

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 11 replies
  • 1889 views
  • 0 likes
  • 5 in conversation