BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
michtka
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

4 REPLIES 4
Astounding
PROC Star

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.

Cynthia_sas
SAS Super FREQ

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;

Tom
Super User Tom
Super User

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';


.


michtka
Fluorite | Level 6

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 874 views
  • 6 likes
  • 4 in conversation