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

@fja @Tom 

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
PaigeMiller
Diamond | Level 26

@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

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

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
Quentin
Super User

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;
The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
TB10
Fluorite | Level 6

@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.

Quentin
Super User

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.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
PaigeMiller
Diamond | Level 26

@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
Tom
Super User Tom
Super User

@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.;

 

Tom
Super User Tom
Super User

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;

SAS Innovate 2025: Register Now

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!

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
  • 937 views
  • 2 likes
  • 4 in conversation