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;
BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
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.

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
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.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

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