I'm currently working on SAS Studio and dealing with macro variables, specifically related to date and integer values. I'm seeking advice and insights from the community on the following:
Macro Variables for Date: How can I effectively utilize macro variables to handle date values in SAS Studio? Any tips, best practices, or examples you can share would be immensely helpful.
Macro Variables for Integer: Similarly, I'm looking for guidance on managing macro variables for integer values. Are there specific considerations or functions that work well for integers in the SAS Studio environment?
Common Challenges: Have you encountered common challenges when working with macro variables for date and integer values in SAS Studio? If so, how did you overcome them?
Code Examples: If you have code snippets or examples demonstrating the usage of macro variables for date and integer values, please feel free to share them.
I believe this discussion will not only assist me in my current project but also serve as a valuable resource for others facing similar questions in the SAS Studio community.
Thank you for your expertise and contributions!
These are quite broad questions, and so the best I can do is give you broad answers. I suspect my answers to 1, 2 and 4 will not meet your needs and so specific (and not broad) questions would help.
/* Question 1 */ %let date=%sysevalf('01NOV2023'd);
/* Question 2 */ %let integer=209413;
Regarding question 1, do not format macro variables that contain dates. See Maxim 28.
Question 3: Work with dates as valid SAS date values, which are the integer number of days since 01JAN1960. Do not work with dates as character strings (such as 11-15-2023) and do not try to pull character strings apart and the glue them back together to get the date in a different format. SAS has done the hard work creating functions, formats and informats to handle almost any date value easily, so you don't have to do this hard work.
Question 4: I can share a gazillion code examples, but I shall refrain until you ask more specific and less broad questions. You could also search these forums, and search the SAS documentation for examples.
First of all in macro language (doesn't matter where [including Studio] it is used) there is only one "data type", it is character. Everything in macro language is a text. If you stick to that thought ("every value is a text") you will be able to avoid a lot of problems.
That is why if you have two macrovariables:
%let a=1;
%let b=2;
and try to "add" their values:
%let c = &a.+&b.;
you will get "1+2" string rather number 3.
The other thing, date in sas is represented as number of days from January 1st 1960. For dates after 1.01.1960 values are positive, and for dates before values are negative.
To get date in "human readable form" you should add format to it.
For example if you set:
%let d = 12345;
text 12345 represent 12,345th day since Jan.1 1960.
Formatting it to human readable for you could use %sysfunc() and trick with int() function:
%let d1 = %sysfunc(int(&d.), date11.);
%let d2 = %sysfunc(int(&d.), yymmdd10.);
%put &=d. &=d1. &=d2.;
and you will see:
D=12345 D1=19-OCT-1993 D2=1993-10-19
in the log.
Next thing is arithmetic. If you have two macrovariables which values "looks like integers" and you want to add, or multiply or subtract you have to use %eval() macro function:
%let c1=%eval(&a. + &b.);
%let c2=%eval(&a. * &b.);
%let c3=%eval(&a. - &b.);
%put &=c1. &=c2. &=c3.;
and you get:
C1=3 C2=2 C3=-1
in the log.
But if you want to divide this becomes more complicated, since the %eval() executes only "integer" operations, so when you run:
%let c4=%eval(&a. / &b.);
%put &=c4.;
you will see:
C4=0
instead 0.5 To overcome "integer limitation" you can use the %sysevalf() macro function, which is design to work with non-integers too.
So after running:
%let c5=%sysevalf(&a. / &b.);
%put &=c5.;
you will see:
C5=0.5
Bart
P.S. When you get some more experience on "how it works" I can also recommend you some useful features of the basePlus package, like %fmt() and %infmt() macros, which can make you work with "formatting macro variables values" a bit easier. But as I wrote, first get more experience with basic macro programming.
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.