Hi all,
I am using the following code to define new variables left_time and Right_time based on the condition for other variables STRUCTURE_NUMBER_008
and count, but the code does not give me the new variables.
I would be glad if anyone could help me to get this fixed.
data Ad_Year.Idaho_nbisteelic04;
set Ad_Year.idaho_nbisteelic03;
by STRUCTURE_NUMBER_008;
if first.STRUCTURE_NUMBER_008 then left_Time=0 & Right_time=count;
if last.STRUCTURE_NUMBER_008 then left_Time=count & Right_time=0;
else Left_time= count & Right_time=count;
run;
I think it should look like this:
data Ad_Year.Idaho_nbisteelic04;
set Ad_Year.idaho_nbisteelic03;
by STRUCTURE_NUMBER_008;
if first.STRUCTURE_NUMBER_008 and last.STRUCTURE_NUMBER_008 then do;
left_Time=0; /* ? */
Right_time=0; /* ? */
end;
else if first.STRUCTURE_NUMBER_008 then do;
left_Time=0;
Right_time=count;
end;
else if last.STRUCTURE_NUMBER_008 then do;
left_Time=count;
Right_time=0;
end;
else do;
Left_time= count;
Right_time=count;
end;
run;
I think it should look like this:
data Ad_Year.Idaho_nbisteelic04;
set Ad_Year.idaho_nbisteelic03;
by STRUCTURE_NUMBER_008;
if first.STRUCTURE_NUMBER_008 and last.STRUCTURE_NUMBER_008 then do;
left_Time=0; /* ? */
Right_time=0; /* ? */
end;
else if first.STRUCTURE_NUMBER_008 then do;
left_Time=0;
Right_time=count;
end;
else if last.STRUCTURE_NUMBER_008 then do;
left_Time=count;
Right_time=0;
end;
else do;
Left_time= count;
Right_time=count;
end;
run;
@PGStats, thank you for your time and nice code. I would like to thank you @s_lassen , @Kurt_Bremser for your constructive comments and hints.
& is a shortcut for "and", which is to be used only in a boolean expression, not for concatenating statements (as you may be used to from UNIX shell programming). Grouping statements into blocks is done with do/end in SAS (equivalent to the curly brackets in C, or begin/end in PASCAL).
As @Kurt_Bremser stated, the & is used for logical AND not for concatenating statements.
left_Time=0 & Right_time=count
gets interpreted as
left_time=(0 and (right_time=Count))
which just assigns 0 (logically false) to left_time, and nothing to right_time.
So, as logical true becomes 1 when used as a numeric you can get what you want with
data Ad_Year.Idaho_nbisteelic04;
set Ad_Year.idaho_nbisteelic03;
by STRUCTURE_NUMBER_008;
left_time=(not first.STRUCTURE_NUMBER_008)*Count;
right_time=(not last.STRUCTURE_NUMBER_008)*Count;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.