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.
@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;
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;
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;
@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.
Again, please show your code.
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 your log for error messages/warnings/bad notes.
@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;
@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.;
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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.