Help using Base SAS procedures

Using a loop to perform a set of steps on each unique value for a given variable?

Reply
Occasional Contributor
Posts: 13

Using a loop to perform a set of steps on each unique value for a given variable?

Hi all,

I have a field called CLIENT_NAME that has several different entries that can change depending on the dataset I'm feeding in.  Because there is room for variation, using a macro to define the CLIENT_NAME values I want to use won't work each time.  Is there a way to use a DO statement to create a loop that will go through every single unique value for a given variable?  For instance, if I have 26 Client Names in a given dataset, the DO statement would run through the associated block of code 26 times, ultimately producing 26 datasets.

Thanks!

Super User
Super User
Posts: 7,076

Using a loop to perform a set of steps on each unique value for a given variable?

Posted in reply to AvocadoRivalry

Normally to generate multiple datasets you will need to use some type of code generation tool.  Either SAS macro code or you can do it yourself with a data step and a %INC.

filename code temp;

data _null_;

  set have ;

  by client_name ;

  if first.client_name;

  file code;

  where = quote(trim(client_name));

  put 'data split_' client_name ';'

     / '  set have;'

     / '  where client_name=' where ';'

    / 'run;'

  ;

run;

%inc code / source2 ;

PROC Star
Posts: 7,492

Using a loop to perform a set of steps on each unique value for a given variable?

Posted in reply to AvocadoRivalry

If the goal is to create separate datasets, and then deal with each separately, why not use a hash? e.g.:

data _null_ ;

  dcl hash hh   (             ) ;

  hh.definekey  ('k'          ) ;

  hh.definedata ('sex', 'name', 'age', 'height', 'weight') ;

  hh.definedone () ;

  do until(mod(k,5)=0 or last);

    k+1;

    set sashelp.class end=last ;

    hh.add();

  end;

  hh.output(dataset: 'a'||strip(k));

run;


Ask a Question
Discussion stats
  • 2 replies
  • 119 views
  • 0 likes
  • 3 in conversation