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
PROC Star

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.
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
PROC Star

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.

Check out the Boston Area SAS Users Group (BASUG) video archives: https://www.basug.org/videos.
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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 255 views
  • 2 likes
  • 4 in conversation