Help using Base SAS procedures

How can I use two variables do loop to create 25 files?

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

How can I use two variables do loop to create 25 files?

How can I create 25 my_class&j&k files, if one of the file (my_class36) from (j,k)=(3,6) is as follows?

 

%LET j=3;
%LET k=6;
data my_class&j&k;
set sashelp.class;
height&j=height*&j;
weight&k=weight*&k;
run;

 

There 25 combinations of j and k, if j=1,3,6,9, & 12 and k=1,3,6,9,& 12.


Accepted Solutions
Solution
‎01-15-2018 10:08 PM
Super User
Posts: 6,762

Re: How can I use two variables do loop to create 25 files?

The previous posters might be right.  This might not be a great idea.  But it is difficult to evaluate unless we know your true intent.  (Certainly you aren't working with sashelp.class in real life.)  Just in case you remain steadfast in pursuing your original question, here is a macro to do that.

 

%macro all25;

 

  %local j_list k_list j k  j_index k_index;

  %let j_list = 1 3 6 9 12;

  %let k_list = 1 3 6 9 12;

 

  %do j_index=1 %to 5;

      %let j = %scan(&j_list, &j_index);

      %do k_index=1 %to 5;

         %let k = %scan(&k_list, &k_index);

         data my_class&j&k;

            set sashelp.class;

            height&j = height * &j;

            weight&k = weight * &k;

         run;

      %end;

%end;

 

%mend all25;

 

%all25

 

The program has to jump through some minor hoops, because macro language imposes restrictions on what forms of the %DO statement are available.

View solution in original post


All Replies
Valued Guide
Posts: 565

Re: How can I use two variables do loop to create 25 files?

Why do you want those datasets? Sounds like a very, very strange requirement. Problem can be solved by using two nested loops in data-null-step or macro.

Super User
Super User
Posts: 9,599

Re: How can I use two variables do loop to create 25 files?

That really is not a good approach.  If you go that way then every proc or datastep you write from that point on will have to be duplicated for each dataset.  Unless its for an export of data or report, its never a good idea to split data up - SAS is geared towards using what is called by groups (which are simpler to program with and faster).  So say for your example, a simpler way would be:

data want;
  set sashelp.class;
  do j=3;
    do k=6;
      output;
    end;
  end;
run;

In this way you have one dataset to work with, with a fixed set of variables (ie. they are all height weight so no need to mess around with arrays), and you can by group on the j and k variables e.g:

proc print data=want;
  by j k;
  title "Group #byval1 section #byval2";
run;

So much simpler than trying to loop each time.

Solution
‎01-15-2018 10:08 PM
Super User
Posts: 6,762

Re: How can I use two variables do loop to create 25 files?

The previous posters might be right.  This might not be a great idea.  But it is difficult to evaluate unless we know your true intent.  (Certainly you aren't working with sashelp.class in real life.)  Just in case you remain steadfast in pursuing your original question, here is a macro to do that.

 

%macro all25;

 

  %local j_list k_list j k  j_index k_index;

  %let j_list = 1 3 6 9 12;

  %let k_list = 1 3 6 9 12;

 

  %do j_index=1 %to 5;

      %let j = %scan(&j_list, &j_index);

      %do k_index=1 %to 5;

         %let k = %scan(&k_list, &k_index);

         data my_class&j&k;

            set sashelp.class;

            height&j = height * &j;

            weight&k = weight * &k;

         run;

      %end;

%end;

 

%mend all25;

 

%all25

 

The program has to jump through some minor hoops, because macro language imposes restrictions on what forms of the %DO statement are available.

Contributor
Posts: 22

Re: How can I use two variables do loop to create 25 files?

Posted in reply to Astounding

I'm sorry to bother all of you, and meanwhile thank you for your support.
I just arbitrarily chose two statements to test if (j,k) can work properly. At the same, my intention for this is that I can easily find the report corresponding to different (j,k).
Astounding’s reply was exactly what we wanted.

 

PROC Star
Posts: 1,283

Re: How can I use two variables do loop to create 25 files?

the solutions provided are probably sound, but I am curious as to why you want to do this?

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 177 views
  • 0 likes
  • 5 in conversation