DATA Step, Macro, Functions and more

Recording an observation

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 86
Accepted Solution

Recording an observation

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


Accepted Solutions
Solution
‎12-09-2012 03:20 AM
Frequent Contributor
Posts: 106

Re: Recording an observation

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


All Replies
Solution
‎12-09-2012 03:20 AM
Frequent Contributor
Posts: 106

Re: Recording an observation

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

Respected Advisor
Posts: 3,799

Re: Recording an observation

Posted in reply to Robert_Bardos

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;
    Frequent Contributor
    Posts: 106

    Re: Recording an observation

    Posted in reply to data_null__

    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.

    🔒 This topic is solved and locked.

    Need further help from the community? Please ask a new question.

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