BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mmhxc5
Quartz | Level 8

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;
1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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;
PG

View solution in original post

4 REPLIES 4
PGStats
Opal | Level 21

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;
PG
mmhxc5
Quartz | Level 8

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

Kurt_Bremser
Super User

& 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).

s_lassen
Meteorite | Level 14

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 4 replies
  • 1463 views
  • 0 likes
  • 4 in conversation