BookmarkSubscribeRSS Feed
Kayla_Tan222
Calcite | Level 5

Hi, how can I do with my code if I want multiple conditions in my if statement. For Ex,

 

if accyr_fy=2019 and accmth_fy<=12 and lossyr_fy=2019-0 or accyr_fy<2019 and lossyr_fy=2019-0

then %let Yearcount=0;

 

else if accyr_fy=2019 and accmth_fy<=12 and lossyr_fy=2019-1 or accyr_fy<2019 and lossyr_fy=2019-1

then %let Yearcount=1;

 

else if accyr_fy=2019 and accmth_fy<=12 and lossyr_fy=2019-2 or accyr_fy<2019 and lossyr_fy=2019-2

then %let Yearcount=2;

 

......

else if accyr_fy=2019 and accmth_fy<=12 and lossyr_fy=2019-9 or accyr_fy<2019 and lossyr_fy=2019-9

then %let Yearcount=9;

 

else accyr_fy=2019 and accmth_fy<=12 and lossyr_fy=2019-10 or accyr_fy<2019 and lossyr_fy=2019-10

then %let Yearcount=10;

 

 

However, this is not work, I don't no whether is because there is a lot of 'and' or 'else if' ot  the way I write is totally wrong.

 

Besides,

I put %let Yearcount=number is because I don't want the Yearcount to be shown in my table. I want to do something with the Yearcount later. I don't no whether this is wrong or correct also. or there is another way to do so?

7 REPLIES 7
33pedro
Fluorite | Level 6

Hi,

 

Part of the problem is the structuring of the if clauses.

 

if accyr_fy=2019 and accmth_fy<=12 and lossyr_fy=2019-0 or accyr_fy=2019 and lossyr_fy=2019-0

then %let Yearcount=0;

 

should really be rewritten as

 

if (accyr_fy=2019 and accmth_fy<=12 and lossyr_fy=2019-0) or (accyr_fy=2019 and lossyr_fy=2019-0)

then %let Yearcount=0;

 

using parentheses. This may not be programatically necessary in every case but it also makes it much easier to read and debug when you can locate 'sections' in each clause. However there is a further problem here where the part after the 'or' effectively overrules the part before. Any obersavtions that satisfy (if accyr_fy=2019 and accmth_fy<=12 and lossyr_fy=2019-0) will also automatically satisfy (accyr_fy=2019 and lossyr_fy=2019-0). So this whole statement effectively is the same as saying.

 

if accyr_fy=2019 and lossyr_fy=2019-0

then %let Yearcount=0;

 

 

Kayla_Tan222
Calcite | Level 5

Hi 33pedro,

First of all thanks for your reply.

 

I found that the error occur is because of the %let statement.

 

do you know the way to hide the yr variable? I dunwan it to be show in my table but then I needed it to do some calculation afterwards.

Patrick
Opal | Level 21

@Kayla_Tan222 

- Use a DROP statement for any variable that you don't need in your output data set (the one in the DATA statement).

- You can't mix SAS data step and SAS macro language (the %LET statement)

- Try and use your year and month variables to create  SAS Date values. This would make it MUCH easier to calculate these year differences.

For your FY vars this would look like: 

acc_date=mdy(accmth_fy,1,accyr_fy);

 

Do you also have a loss month variable? Or could we just use a date beginning of the year?

 

For the logic you're trying to implement: Is the difference just year boundaries or should it be an actual full year difference?

33pedro
Fluorite | Level 6

I am not too sure about better ways to deal with the Yearcount macro variable but I also would say that you are assigning new values to the same macro variable each time. Someone else may be better placed to advise on this though

Patrick
Opal | Level 21

@33pedro 

"that you are assigning new values to the same macro variable each time"

It's actually even worse: SAS Macro code executes before the data step. So you can't have a %LET statement in a SAS data step IF condition (that throws a syntax error). But even if you could: The %LET statement would execute before the data step and though the value of the macro variable would be set to the last assignment of it (the last %let statement).

 

You need to use CALL SYMPUT() to write a value from a SAS data step to macro level. You then can only use the value of this macro variable AFTER the data step finished (=only after the RUN; )

Reeza
Super User

This is not correct either, or at least I highly suspect it won't give you what you need. 

 

lossyr_fy=2019-0 

You either need to wrap that in quotes because it's a character variable or determine what the underlying value is and use that.

 


@Kayla_Tan222 wrote:

Hi, how can I do with my code if I want multiple conditions in my if statement. For Ex,

 

if accyr_fy=2019 and accmth_fy<=12 and lossyr_fy=2019-0 or accyr_fy<2019 and lossyr_fy=2019-0

then %let Yearcount=0;

 

else if accyr_fy=2019 and accmth_fy<=12 and lossyr_fy=2019-1 or accyr_fy<2019 and lossyr_fy=2019-1

then %let Yearcount=1;

 

else if accyr_fy=2019 and accmth_fy<=12 and lossyr_fy=2019-2 or accyr_fy<2019 and lossyr_fy=2019-2

then %let Yearcount=2;

 

......

else if accyr_fy=2019 and accmth_fy<=12 and lossyr_fy=2019-9 or accyr_fy<2019 and lossyr_fy=2019-9

then %let Yearcount=9;

 

else accyr_fy=2019 and accmth_fy<=12 and lossyr_fy=2019-10 or accyr_fy<2019 and lossyr_fy=2019-10

then %let Yearcount=10;

 

 

However, this is not work, I don't no whether is because there is a lot of 'and' or 'else if' ot  the way I write is totally wrong.

 

Besides,

I put %let Yearcount=number is because I don't want the Yearcount to be shown in my table. I want to do something with the Yearcount later. I don't no whether this is wrong or correct also. or there is another way to do so?


 

Tom
Super User Tom
Super User

Please provide a little more context.  In addition to the mixed up logic of the IF conditions you have mixed macro code (%LET) with data step code (IF).  Provide sample input and expected output data for the example input.

 

Why do you think you need a macro variable?  What code are you going to use the macro variable to generate?  

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 986 views
  • 1 like
  • 5 in conversation