BookmarkSubscribeRSS Feed
mansour_ib_sas
Pyrite | Level 9

Hello,

I want to use the go to following condition function.
Below an example.
the results of both functions is incorrect.
Could you, please, help me understand why?

Thank you

 

 

data info;
   input x;
   if 1<=x<=5 then do;
   go to eq1;
   return;    
  end;
   
   else do;
     if x > 6 then do;
     go to eq2;
     return;
     end;
   end;
   eq1: eq_x1=x;
   eq2: eq_x2=x;
  
   datalines;
7
6
323
2
5
;
7 REPLIES 7
Kurt_Bremser
Super User

First, let's give this code some visual formatting so we can understand its structure (see Maxim 12):

data info;
input x;
if 1 <= x <= 5 then do;
  go to eq1;
  return;    
end;
else do;
  if x > 6 then do;
    go to eq2;
    return;
  end;
end;
eq1: eq_x1 = x;
eq2: eq_x2 = x;
datalines;
7
6
323
2
5
;

Next, it is important to keep in mind that go to is not like a subroutine call; everything after the targeted label will be executed. This means that every time the first option takes effect, both assignments will execute.

On top of that, if no condition is met, the execution will simply "fall through" and also execute both assignments.

Only in cases where x > 6 will the second assignment alone be executed.

 

What did you expect?

 

Hint from me:

In 20+ years of working with SAS, I have never (as in NEVER) had to use a go to. There may be cases where a %goto can be helpful, but a data step go to solves nothing IMO that does not have a much better, structured solution by using do/end blocks and while/until loops.

Astounding
PROC Star

Is it possible you are overcomplicating things?

 

data info;
   input x;
   if 1<=x<=5 then eq_x1 = x;
   else if x > 6 then eq_x2 = x;
   datalines;
7
6
323
2
5
;
mansour_ib_sas
Pyrite | Level 9

I have dealt with a similar case.
I want to go find the difference between varc and Avc of E1 in E2.
for that, I take this difference in av of E2 when varC equal to 2 then the inputer in av of E1 to varc = 1

 

 

 

data HA;
input varA varB VarC qdT av avT;
cards;
AA E1 1 10 4  8
AA E1 3 10 4  8
AA E2 2 5  10 15 
AA E2 4 5  5  15
; run;

data WA;
input varA varB VarC qdT av avC;
cards;
AA E1 1 10 6  10
AA E1 3 10 4  10
AA E2 2 5  8  13 
AA E2 4 5  5  13
; run;


data HB;
input varA varB VarC qdT av avC;
cards;
AA E1 1 10 4  8
AA E1 3 10 4  8
AA E2 2 5  1  6 
AA E2 4 5  5  6
; run;


data WB;
input varA varB VarC qdT av avC;
cards;
AA E1 1 10 5  9
AA E1 3 10 4  9
AA E2 2 5  0  6 
AA E2 4 5  5  6
; run;

Reeza
Super User
Is that your input data sets? All 4? From those demo data sets what do you expect as output?
mansour_ib_sas
Pyrite | Level 9

 HA is input and WA output expect

 

HB is input and WB output  expect

Astounding
PROC Star

It has been a couple of days, and I still haven't figured out the calculations.

 

How do you get from HA to WA (and from HB to WB)?  I don't need programming statements, I need to see that logic that you use and where the numbers come from.

ballardw
Super User

@mansour_ib_sas wrote:

 HA is input and WA output expect

 

HB is input and WB output  expect


Your statements:

I want to go find the difference between varc and Avc of E1 in E2.
for that, I take this difference in av of E2 when varC equal to 2 then the inputer in av of E1 to varc = 1

Appear to be comparing variables in different data sets since AVC is NOT in HA (input) where does the value of AVC come from that is compared with VARC to get a difference???

 

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
  • 957 views
  • 0 likes
  • 5 in conversation