Help using Base SAS procedures

How do I use a macro variable inside a IN statement

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 82
Accepted Solution

How do I use a macro variable inside a IN statement

Hi,

I am trying to do the following:

1)  Read in a variable from the data set and

2)  Create a macro variable from it to be able to,

3)  Put in an IF statement that can be used on the left hand side of the IN part of the IF statement.

See the following program.

I was wondering if I can some how execute the second IF statement instead of the first one?

It works correctly with the first one, but originally I thought of the first one and tried to

make it work.  I never could figure it out.  What could I use instead of the SYMPUT?

Thanks,

Nancy

Attachment

Accepted Solutions
Solution
‎12-04-2012 09:38 AM
Super User
Super User
Posts: 7,079

Re: How do I use a macro variable inside a IN statement

Posted in reply to SASHunter

Not sure why you want the macro variable?  Usually you would use macro variables to dynamically generate text that you could not code directly. But your example just looks like you want to calculate a variable and then compare its value to another existing variable. No need to dynamically generate code. Looks like you want something like this. (Note I renamed newage to newyear as it seemed a better indication of the content of the variable.)

data temp;

  input name $  pebirthy pusf2 ;

  newyear= pebirthy + 12;

  if newyear <= PUSF2 < 2012 then flag = 'Y';

  else flag = 'N';

  put (name pebirthy pusf2 newyear flag) (=);

cards;

Tina  1985 -1

Betty  1975 1990

Sue 1951 1961

Sally 1988 2011

Marylu 1964 2001

Janet 1978 1997

Jean 1995 2006

run;

name=Tina pebirthy=1985 pusf2=-1 newyear=1997 flag=N

name=Betty pebirthy=1975 pusf2=1990 newyear=1987 flag=Y

name=Sue pebirthy=1951 pusf2=1961 newyear=1963 flag=N

name=Sally pebirthy=1988 pusf2=2011 newyear=2000 flag=Y

name=Marylu pebirthy=1964 pusf2=2001 newyear=1976 flag=Y

name=Janet pebirthy=1978 pusf2=1997 newyear=1990 flag=Y

name=Jean pebirthy=1995 pusf2=2006 newyear=2007 flag=N

Now if you do not need to store the value of birth year + 12 then you can just include the addition in the conditional used in the IF statement and eliminate the creation of a new variable.

  if pebirthy + 12 <= PUSF2 < 2012 then flag = 'Y';

  else flag = 'N';

View solution in original post


All Replies
Super User
Posts: 19,877

Re: How do I use a macro variable inside a IN statement

Posted in reply to SASHunter

I'm not familiar with the following:

in (&z:2012)

What are you expecting that to do?

Can you explain a bit more what you're trying to do, are you sure you need macro variables?

Frequent Contributor
Posts: 82

Re: How do I use a macro variable inside a IN statement

Hi,

What I am trying to do is process each record and read the birth year of the mother (pebirthy).  Than add twelve to it.

Now I want to use this 'newage' year and put it into a macro variable, which will be updated for each record.  Than I will test to see if the year they said they had their first child (pusf2) falls in between the new macro variable and 2012.

So the line that I have commented out -  '  If pusf2 IN ( &z : 2012 ) then  '

was the first thing I thought about because of the shorten code, but of course it doesn't work. Unless someone can show me that it does.

And thanks ballardw - your right, I need the <=  sign before the 2012.

Thanks for your input.

Super User
Posts: 11,343

Re: How do I use a macro variable inside a IN statement

Posted in reply to SASHunter

Generally you can't use a macro variable in the datastep that creates it.

What is the problem with using the following if it gets the output you need?

 

if PUSF2 >= newage AND PUSF2 < 2012 then  /* other than it should be <= if you want to consider it equal to the attempted IN statement*/

or

if newage <= PUSF2 <= 2012

And I also have wanted more than once for IN to accept variables instead of only literal values.

Contributor
Posts: 28

Re: How do I use a macro variable inside a IN statement

Posted in reply to SASHunter


this seems like a case option in proc sql.

or using your new column in a datastep...

proc sql;

     create table temp2 as select *, pebirthy+12 as newage, case when (pebirthy+12) le 2012 then 'yes' else 'nooo' end from temp;

quit;

I'm just missing the last calculation but it's close

Solution
‎12-04-2012 09:38 AM
Super User
Super User
Posts: 7,079

Re: How do I use a macro variable inside a IN statement

Posted in reply to SASHunter

Not sure why you want the macro variable?  Usually you would use macro variables to dynamically generate text that you could not code directly. But your example just looks like you want to calculate a variable and then compare its value to another existing variable. No need to dynamically generate code. Looks like you want something like this. (Note I renamed newage to newyear as it seemed a better indication of the content of the variable.)

data temp;

  input name $  pebirthy pusf2 ;

  newyear= pebirthy + 12;

  if newyear <= PUSF2 < 2012 then flag = 'Y';

  else flag = 'N';

  put (name pebirthy pusf2 newyear flag) (=);

cards;

Tina  1985 -1

Betty  1975 1990

Sue 1951 1961

Sally 1988 2011

Marylu 1964 2001

Janet 1978 1997

Jean 1995 2006

run;

name=Tina pebirthy=1985 pusf2=-1 newyear=1997 flag=N

name=Betty pebirthy=1975 pusf2=1990 newyear=1987 flag=Y

name=Sue pebirthy=1951 pusf2=1961 newyear=1963 flag=N

name=Sally pebirthy=1988 pusf2=2011 newyear=2000 flag=Y

name=Marylu pebirthy=1964 pusf2=2001 newyear=1976 flag=Y

name=Janet pebirthy=1978 pusf2=1997 newyear=1990 flag=Y

name=Jean pebirthy=1995 pusf2=2006 newyear=2007 flag=N

Now if you do not need to store the value of birth year + 12 then you can just include the addition in the conditional used in the IF statement and eliminate the creation of a new variable.

  if pebirthy + 12 <= PUSF2 < 2012 then flag = 'Y';

  else flag = 'N';

Frequent Contributor
Posts: 82

Re: How do I use a macro variable inside a IN statement

Tom,

Thanks for your input.  I like your bottom example, because 'No' I don't really want an extra variable.  So I'll recode it as you put it in the bottom of your post.

Thanks for your time.

Frequent Contributor
Posts: 82

Re: How do I use a macro variable inside a IN statement

Tom (or anyone)

How do I mark my questions with 'answered'  - where it says 'This question is:'  ?


New Contributor
Posts: 3

Re: How do I use a macro variable inside a IN statement

Posted in reply to SASHunter

Though this thread is answered, I once was in trouble until I found this paper

http://www.nesug.org/Proceedings/nesug09/cc/cc05.pdf

and used it as below-

options minoperator; 

%if %eval(&D in 07,14,21,28,35,42,49,56,63,70,77,84,91) %then %do;

🔒 This topic is solved and locked.

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

Discussion stats
  • 8 replies
  • 406 views
  • 4 likes
  • 6 in conversation