Fluorite | Level 6

## Multiple then from single if condition

so i have a scenario as follows:

a =intnx('day',today(),-1);

b=intnx('day',today(),-2);

we have a variable named flag whose value is set based on other conditions.

now what is desired is: suppose flag=1:

if flag=1 then a =intnx('day',today(),-1) and b=intnx('day',today(),-2);

so the above syntax is desired but looks like we cannot add multiple then conditions through and.

i tried with if..do statements but after we end the statement i cannot access the variable a & b after that. can you please help me here.

1 ACCEPTED SOLUTION

Accepted Solutions
Diamond | Level 26

## Re: Multiple then from single if condition

@TB10 wrote:

@PaigeMiller @Quentin  i tried this but i am unable to check the values of a and b when i write put a; put b; out side of the loop. Anyway to check the values. As mentioned i am unable to access the variable values outside the loop.

``````if flag=1 then do;
a =intnx('day',today(),-1);
b=intnx('day',today(),-2);
put a= b=;
end;``````
--
Paige Miller
7 REPLIES 7
Diamond | Level 26

## Re: Multiple then from single if condition

if flag=1 then a =intnx('day',today(),-1) and b=intnx('day',today(),-2);

``````if flag=1 then do;
a =intnx('day',today(),-1);
b=intnx('day',today(),-2);
end;``````
--
Paige Miller
PROC Star

## Re: Multiple then from single if condition

Please show the code you tried, along with some test data.  Using IF THEN DO should work, like:

``````if flag=1 then do;
a=intnx('day',today(),-1);
b=intnx('day',today(),-2);
end;``````
Check out the Boston Area SAS Users Group (BASUG) video archives: https://www.basug.org/videos.
Fluorite | Level 6

## Re: Multiple then from single if condition

@PaigeMiller @Quentin  i tried this but i am unable to check the values of a and b when i write put a; put b; out side of the loop. Anyway to check the values. As mentioned i am unable to access the variable values outside the loop.

PROC Star

## Re: Multiple then from single if condition

``````data _null_;
set have;
if flag=1 then do;
a=intnx('day',today(),-1);
b=intnx('day',today(),-2);
end;
put a= b=;
run;``````

Check out the Boston Area SAS Users Group (BASUG) video archives: https://www.basug.org/videos.
Diamond | Level 26

## Re: Multiple then from single if condition

@TB10 wrote:

@PaigeMiller @Quentin  i tried this but i am unable to check the values of a and b when i write put a; put b; out side of the loop. Anyway to check the values. As mentioned i am unable to access the variable values outside the loop.

``````if flag=1 then do;
a =intnx('day',today(),-1);
b=intnx('day',today(),-2);
put a= b=;
end;``````
--
Paige Miller
Super User

## Re: Multiple then from single if condition

@TB10 wrote:

@PaigeMiller @Quentin  i tried this but i am unable to check the values of a and b when i write put a; put b; out side of the loop. Anyway to check the values. As mentioned i am unable to access the variable values outside the loop.

Perhaps you intended the values of A and B to be retained across iterations (in a simple data step that is the same as across observations) of the data step.  Add a RETAIN statement.

``retain A B ;``

You also need to have a FORMAT statement so the date values will print in a human recognizable style.

``format A B yymmdd10.;``

Super User

## Re: Multiple then from single if condition

ADD is a logical operator.  So unless variable B already has the date that is TODAY()-2 then this statement:

``a =intnx('day',today(),-1) and b=intnx('day',today(),-2);``

will set A to zero.  The first expression

``intnx('day',today(),-1)``

Which can more easily be written as:

``(today()-1)``

Is only false if today is 02JAN1960.

But the second condition:

``b=(today()-2)``

can only be true if the variable B already has a value that is the number used by SAS to present the day before yesterday.

You want to execute the DO statement in the IF/THEN statement.  This will allow you execute multiple statements when the IF condition is satisfied.

``````if flag=1 then do;
a =today()-1;
b = a-1;
end;``````
Discussion stats
• 7 replies
• 255 views
• 2 likes
• 4 in conversation