Macro Code

Accepted Solution Solved
Reply
Super Contributor
Super Contributor
Posts: 440
Accepted Solution

Macro Code

hi all,

The dataset orders gets created ,nothing's wrong my infile but my micro code not working.Anyone knows why,please?

PS.at least for sysday=wed should work but it doesnot. And in additon to this can i use the second code below for the same purpose.Will it work.Can i use automatic macro variables within a sas code?

options mprint;

DATA orders;

INFILE 'C:\Users\Trajce\Contacts\Desktop\Orders.txt';

INPUT CustomerID $ 1-3 OrderDate DATE7. Model $ 13-24 Quantity;

RUN;

%MACRO reports;

%IF &SYSDAY = Monday %THEN %DO;

PROC PRINT DATA = orders NOOBS;

FORMAT OrderDate DATE7.;

TITLE "&SYSDAY Report: Current Orders";

%END;

%ELSE %IF &SYSDAY = WEDNESDAY %THEN %DO;

PROC TABULATE DATA = orders;

CLASS CustomerID;

VAR Quantity;

TABLE CustomerID ALL, Quantity;

TITLE "&SYSDAY Report: Summary of Orders";

%END;

%MEND reports;

RUN;

%reports

RUN;

second code:

DATA orders;

INFILE 'C:\Users\Trajce\Contacts\Desktop\Orders.txt';

INPUT CustomerID $ 1-3 OrderDate DATE7. Model $ 13-24 Quantity;

IF &SYSDAY = Monday THEN DO;

PROC PRINT DATA = orders NOOBS;

FORMAT OrderDate DATE7.;

TITLE "&SYSDAY Report: Current Orders";

END;

ELSE IF &SYSDAY = WEDNESDAY THEN DO;

PROC TABULATE DATA = orders;

CLASS CustomerID;

VAR Quantity;

TABLE CustomerID ALL, Quantity;

TITLE "&SYSDAY Report: Summary of Orders";

END;

RUN;


Accepted Solutions
Solution
‎05-01-2013 02:06 PM
Super User
Posts: 17,912

Re: Macro Code

Are you running in EG or Base?

What does the log say when you run that line of code.

%put &sysday;

View solution in original post


All Replies
Super User
Posts: 17,912

Re: Macro Code

SAS is case sensitive.

It should be Wednesday not WEDNESDAY.

Super Contributor
Super Contributor
Posts: 440

Re: Macro Code

it was actually Wednesday  and then changed to WEDNESDAY.Did not work in either way

Super User
Posts: 17,912

Re: Macro Code

output &sysday and see what it says then.

Super Contributor
Super Contributor
Posts: 440

Re: Macro Code

Sorry Reeza.Dont know what you mean.

@Ballard

"If you aren't closing and restarting SAS daily then you would have unexpected values.Second is to insure the proper case when comparing using equal signs"

Is it recommended that i restart my SAS daily for Macros in order to work?

Solution
‎05-01-2013 02:06 PM
Super User
Posts: 17,912

Re: Macro Code

Are you running in EG or Base?

What does the log say when you run that line of code.

%put &sysday;

Super Contributor
Super Contributor
Posts: 440

Re: Macro Code

it is BASE and %put &sysday is giving me Thursday....Weird but thank you guys a lot Smiley Happy

Super User
Posts: 10,538

Re: Macro Code

It's reporting the date information from the computer system clock from when the SAS session started. If you're system date isn't Thursday but you started SAS and didn't shut it down since last week you could get Thursday. Try %put &sysdate as well. If the date isn't today you have another clue.

Super Contributor
Super Contributor
Posts: 440

Re: Macro Code

ok my SAS just resolved sysday to be Thursday and  when i changed to sysday=thursday i did get results.

could this  something to do  with my PC settings ?

Super User
Posts: 10,538

Re: Macro Code

One issue could be that SYSDAY has the value the SAS session started. If you aren't closing and restarting SAS daily then you would have unexpected values.Second is to insure the proper case when comparing using equal signs. When I resolved SYSDAY on my system the result was Wednesday" not the WEDNESDAY used in your code. You may want to use %upcase(&sysday)=WEDNESDAY or use the case matching SYSDAY.


Contributor
Posts: 61

Re: Macro Code

hi,

Can any one show me why Monday does not need to be in quote as

%IF &SYSDAY = "Monday" %THEN %DO;

?


Thanks

Super User
Posts: 10,538

Re: Macro Code

In the MACRO processor everything is strings. So if you are comparing a value quotes are not required, unless the value you want needs them.

This is one reason for using "&macrovariable" in data steps instead of having the macro variable include quotes.

Contributor
Posts: 61

Re: Macro Code

can you please illustrate with examples? My understanding is that macro variables are always in quotes when used in the TITLE, FOOTNOTE statements. But when they are quoted in other statements, it becomes confusing. Thanks

Super User
Posts: 10,538

Re: Macro Code

In a title or other places you would use a text literal:

%let val = Idaho;

Title " Report for &val"; which resolve to Title " Report for Idaho"; from the macro processor.

If you devine val as "Idaho" then one of two things are likely happen:

If you use

title 'Report for &val'; you don't resolve the macro variable because it is within single quotes.

If you use

title "Report for &val"; then the macro processor produces: Title "Report for "Idaho" "; and Idaho is OUTSIDE the quoted string required for title and isn't one of the acceptable options for title and will generate an error. There are ways to use the "value" definition but they get to be a lot of work, especially if you ever want to combine more than one macro variable in a statement.

In a data step:

data want;

     set have;

     if state = "&val" then do; other code.

In this example you original approach would work. But This way you can tell immediately in the code that we are expecting a text literal (and you enhanced program editor will help with that).

But suppose I want the macro variable to reference a variable name:

%let var = Total_Sales;

Data want;

     set have;

     Someothervariable = &var * 1.05;

☑ This topic is solved.

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

Discussion stats
  • 13 replies
  • 637 views
  • 5 likes
  • 4 in conversation