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

Hi

Consider a Table:

ID Var1 Var2 Var3 Var4 Var5

1   3       3     3       4      4

2   2       2     2       3      3

I would like to see, when is the first time value 3 has generated in var1 - var5

My Method to do it is following:

Data Need;

Set Have;

Array check(*) Var1 - Var5

Do i = 1 to 5;

if Check(i) = 3 then do;

Flag = i; goto : en;

output;

end;

en: Run;

Goto statement helps me come out of the loop first time my condition satisties. It might not be a sophisticated way to write code but it works.  Any suggestions how this can be improved or something alternative?

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Robert_Bardos
Fluorite | Level 6

Hi "Bhpinder",

basically you are doing the right thing, but yes, it can be improved.

  • to exit a do loop use the LEAVE statement (i.e. you can drop GOTO and its target label)
  • use 'do i=1 to dim(check);' thus bypassing hard coding the number of elements in the array

further possibilities for improvement:

  • loop exit can also be controlled on the DO statement itself, so look up
    • DO UNTIL and
    • DO WHILE respectively
  • some people prefer using automatic variables (e.g. _n_) for the loop counter because
    • _n_ is automatically dropped i.e. not written to the eventual dataset
    • modifying _n_ does not interfere with the way _n_ gets assigned by the datastep's internal mechanisms so it can be used rather freely
      (Don't waste too much time thinking about this statement if its meaning is not immediately understandable.
      I'm sure it can be expressed much better but I'm getting to my limits here with regards to my command of the english language.)
      (PS: I'm not an advocate of this use of _n_ as it might confuse people for whom SAS is but one of the many tools they use.)

Generally I strongly advice to bookmark SAS online documentation. My favourite (not having had access to any SAS system above 9.1.3) being - you guess it - the 9.1.3 Documentation and there especially the Language Reference Dictionary book. SAS has such a rich set of functions, formats, informats, options and statements that skimming through its pages is time well invested. Furthermore there are overview pages in this document such as Functions and CALL Routines : Functions and CALL Routines by Category that I always turn to when a voice inside me says "there must be some function, format, whatsoever that exactly does this".

Kind regards

Robert

View solution in original post

3 REPLIES 3
Robert_Bardos
Fluorite | Level 6

Hi "Bhpinder",

basically you are doing the right thing, but yes, it can be improved.

  • to exit a do loop use the LEAVE statement (i.e. you can drop GOTO and its target label)
  • use 'do i=1 to dim(check);' thus bypassing hard coding the number of elements in the array

further possibilities for improvement:

  • loop exit can also be controlled on the DO statement itself, so look up
    • DO UNTIL and
    • DO WHILE respectively
  • some people prefer using automatic variables (e.g. _n_) for the loop counter because
    • _n_ is automatically dropped i.e. not written to the eventual dataset
    • modifying _n_ does not interfere with the way _n_ gets assigned by the datastep's internal mechanisms so it can be used rather freely
      (Don't waste too much time thinking about this statement if its meaning is not immediately understandable.
      I'm sure it can be expressed much better but I'm getting to my limits here with regards to my command of the english language.)
      (PS: I'm not an advocate of this use of _n_ as it might confuse people for whom SAS is but one of the many tools they use.)

Generally I strongly advice to bookmark SAS online documentation. My favourite (not having had access to any SAS system above 9.1.3) being - you guess it - the 9.1.3 Documentation and there especially the Language Reference Dictionary book. SAS has such a rich set of functions, formats, informats, options and statements that skimming through its pages is time well invested. Furthermore there are overview pages in this document such as Functions and CALL Routines : Functions and CALL Routines by Category that I always turn to when a voice inside me says "there must be some function, format, whatsoever that exactly does this".

Kind regards

Robert

data_null__
Jade | Level 19

The function is WHICHN

data find3;
   array v[5];
   input id $ v
  • ;
  •    find3 = whichN(3,of v
  • );
  •    cards;
    1   3       3     3       4      4
    2   2       2     2       3      3
    ;;;;
       run;
    Robert_Bardos
    Fluorite | Level 6

    Great! Thanks, data _null_,

    now that I have access to "my" z/OS system again, I verified that both search type functions (WHICHC/WHICHN) though not documented do work with SAS 9.1.3 as well (z/OS 1.12 here with SAS 9.1.3, &sysvlong displaying - retyped - 9.01.01M3P020206).

    Added the 9.2 categorized list of functions to my bookmarks.

    SAS Innovate 2025: Register Now

    Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
    Sign up by Dec. 31 to get the 2024 rate of just $495.
    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.

    SAS Training: Just a Click Away

     Ready to level-up your skills? Choose your own adventure.

    Browse our catalog!

    Discussion stats
    • 3 replies
    • 1094 views
    • 6 likes
    • 3 in conversation