DATA Step, Macro, Functions and more

Setting length for variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Setting length for variable

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!


Accepted Solutions
Solution
‎03-17-2017 02:49 PM
Super User
Posts: 6,972

Re: Setting length for variable

Use this

 call symput('timeofday',trim(timeofday));

when creating the macro variable.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 17,963

Re: Setting length for 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.

PROC Star
Posts: 7,366

Re: Setting length for variable

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

 

Occasional Contributor
Posts: 7

Re: Setting length for variable

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"

.....

Super User
Posts: 17,963

Re: Setting length for variable

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;

Occasional Contributor
Posts: 7

Re: Setting length for variable

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

Occasional Contributor
Posts: 7

Re: Setting length for variable

[ Edited ]

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.

Super User
Posts: 5,099

Re: Setting length for variable

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

Super User
Posts: 17,963

Re: Setting length for variable

call symputX('timeofday',timeofday);
Solution
‎03-17-2017 02:49 PM
Super User
Posts: 6,972

Re: Setting length for variable

Use this

 call symput('timeofday',trim(timeofday));

when creating the macro variable.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 7

Re: Setting length for variable

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

Super User
Posts: 6,972

Re: Setting length for variable

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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