BookmarkSubscribeRSS Feed
nexusiceland
Calcite | Level 5

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:

  1. 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.

  2. 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?

  3. 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?

  4. 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!

6 REPLIES 6
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
yabwon
Onyx | Level 15

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.

 

 

 

 

 

 

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



s_lassen
Meteorite | Level 14

How can I effectively utilize macro variables to handle date values in SAS Studio?

I normally use the DATE9. format for date macro variables. Either plain text, or as date constant.

%let date1=31DEC2022; /* plain text */
%let date2='31DEC2022'd; /* Date constant */

The way to use these macro variables is simple:

data test;
  retain date1 "&date1"d; /* convert to date constant */
  date2=&date2; /* already formatted as date constant */
  datetime="&date1:00:00"dt; /* a datetime, not possible with the second format */
  datetime2=dhms(&date2,0,0,0); /* but you can do it like this */
  /* more statements */
run;

Note the double quotes, as macro expressions within single quotes are not resolved.

 

They can also be created using functions with %SYSFUNC, e.g.

%let date1=%sysfunc(today(),date9.);
%let date2="%sysfunc(today(),date9.)"d;

I much prefer to have my date macro variables in a readable format, rather than integers, as they are easier to understand and read, for instance in MPRINT output.

 

yabwon
Onyx | Level 15

From macro language point of view they both:

%let date1=31DEC2022;
%let date2='31DEC2022'd;

are plain text 😉 😉 😉

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

You use the macro language to generate SAS code.  Macro variables hold text.  So make them hold the text that will generate valid SAS code.

 

So if want use the macro variable to display a date value for humans to read then put in text that humans will understand.

%let today=%sysfunc(today(),date9.);

Now you can use that string to generate a title for your report.

title "Summary Report rundate: &today";

But you could not use it as if it was an actual date value since code like:

rundate = &today ;

Would generate invalid SAS code 

rundate = 04JAN2024 ;

You could fix it to generate valid SAS code by adding more to your macro program.

rundate = "&today"d;

But that only works if the string that is in the macro variable TODAY is something that the DATE informat can convert to an actual date value.

ballardw
Super User

@nexusiceland wrote:

 

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.

 


Search the forum for the words MACRO and DATE. There should be box showing text "Search the community" at the top of the page. Type those words into to the box.  Click on the search icon.

 

Be prepared to wade through 100's if not 1000's of topics...

You will find many examples of using the macro variables as formatted text, i.e. 2024-01-04 and the problems resulting. 

About the only time I recommend formatting macro variable dates, times or datetime variables is when people have to read them such as in title statements or file names on output.

 

Most of the examples of general use will be in the Programming community.

 

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1215 views
  • 6 likes
  • 6 in conversation