Help using Base SAS procedures

logical expresion macro

Accepted Solution Solved
Reply
Super Contributor
Posts: 301
Accepted Solution

logical expresion macro

Hy everyone,

how can I write this expression inside of a macro,

%if stat in ('MEAN','MIN','MAX') %then format &macrovariable 6.2;....(I know I can write three times  %if  stat= ' X'  %then ....) but, 

are the logical (in, not in, or, and) recognize inside of a macro?:

Thanks.


Accepted Solutions
Solution
‎06-04-2012 11:02 AM
Super User
Posts: 5,495

Re: logical expresion macro

Michtka,

It looks like you are trying to make SAS do things that it just won't do.  It looks like you are trying to process a SAS data set using macro language.

Just for the record, macro language does contain an IN operator, usually using the # character.  It was introduced in 9.2, but not really working until 9.3.  So you have to be running 9.3 to be able to use it.

The bigger point though is that it seems you are trying to process a SAS data set using macro language.  That just won't happen.  For example, a macro language statement that begins %if stat = 'MEAN' will compare four characters ("stat") to six characters ("'MEAN'") and find that they are never equal.  I suspect that you have a SAS data set that contains a variable named STAT.  A macro language %IF statement will only examine the value of a macro variable, never the value of a DATA step variable.  A secondary issue would be that a format for a DATA step variable cannot change from one observation to the next.  The format applies to the entire column of data.

If I've interpreted this wrong, and your problem is really something else, you can always add a description of your inputs and desired outputs.

Good luck.

View solution in original post


All Replies
Solution
‎06-04-2012 11:02 AM
Super User
Posts: 5,495

Re: logical expresion macro

Michtka,

It looks like you are trying to make SAS do things that it just won't do.  It looks like you are trying to process a SAS data set using macro language.

Just for the record, macro language does contain an IN operator, usually using the # character.  It was introduced in 9.2, but not really working until 9.3.  So you have to be running 9.3 to be able to use it.

The bigger point though is that it seems you are trying to process a SAS data set using macro language.  That just won't happen.  For example, a macro language statement that begins %if stat = 'MEAN' will compare four characters ("stat") to six characters ("'MEAN'") and find that they are never equal.  I suspect that you have a SAS data set that contains a variable named STAT.  A macro language %IF statement will only examine the value of a macro variable, never the value of a DATA step variable.  A secondary issue would be that a format for a DATA step variable cannot change from one observation to the next.  The format applies to the entire column of data.

If I've interpreted this wrong, and your problem is really something else, you can always add a description of your inputs and desired outputs.

Good luck.

SAS Super FREQ
Posts: 8,861

Re: logical expresion macro

Posted in reply to Astounding

Hi:

  Astounding is correct, that you cannot test data set variables in a macro program. However, using PROC REPORT, you can apply one format for one value in a report row, versus another format for a different value. For example, suppose I use PROC SUMMARY on SASHELP.SHOES to get the standard 5 statistics into a data set. Then I can format each statistic separately using CALL DEFINE in PROC REPORT. This is a REPORT (not a dataset) generated by PROC REPORT. Notice the different formats for each report row for the Sales column, but the condition for the format was based on the value of the _STAT_ column (created by PROC MEANS/SUMMARY):

Stat             Sales

N                  395

MIN            $325.00

MAX       1,298,717.00

MEAN        $85,700.17

STD       129,107.2339

Not quite what the OP originally described, but PROC REPORT does have this flexibility. Code below.

cynthia

proc summary data=sashelp.shoes;

var sales;

output out=work.statsales / autoname;

run;

     

proc report data=work.statsales nowd;

  column _stat_ sales;

  define _stat_ / display 'Stat';

  define sales / display 'Sales';

  compute sales;

    if _stat_ = 'N' then

       call define(_col_,'format','comma6.');

    else if _stat_ in ('MIN', 'MEAN') then

       call define(_col_,'format','dollar16.2');

    else if _stat_ = 'MAX' then

       call define(_col_,'format','comma16.2');

    else if _stat_ = 'STD' then

       call define(_col_,'format','comma20.4');

  endcomp;

run;

Super User
Super User
Posts: 7,035

Re: logical expresion macro

Sounds like you want data step to reformat data differently based on value in another variable.

You will need to store this into a character variable. You can use the PUTN() function to do this.


if stat in ('MEAN','MIN','MAX') then charvar= putn(numvar,"6.2");

else if stat = 'N' then charvar=putn(numvar,6.);

else charvar='UNK STAT';


.


Super Contributor
Posts: 301

Re: logical expresion macro

Sorry Guys, it was a mistake...now i understood the concept of using macrovariables with the Astounding's explanation.

Yes Tom, it was my idea.

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 267 views
  • 6 likes
  • 4 in conversation