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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

5 REPLIES 5
andreas_lds
Jade | Level 19

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Astounding
PROC Star

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.

vickyCh
Obsidian | Level 7

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.

 

PeterClemmensen
Tourmaline | Level 20

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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