0

I would like to create a list of IDs (bee_created) that combine a treatment group (i), replicate (j) and serial number (k). I have 9 replicates to work on, each with slight variations in serial numbers in the treatment groups. So it would a lot more efficient to create all the IDs in macro do loops. I've browsed some articles and I suppose that %scan may be used but I haven't been able to produce working codes.

For each replicate, there are five treatment groups 'aa','ab', 'ac', 'ea' and 'ec'. I am needing a macro that could replace the following 4 data sets. Note the only differences are in the values of i, j and k; else are copy-and-paste. Thanks in advance

/*dataset 1*/
data tag_num_replicate01_02;
    length i $5. bee_created $9.;
    exp_name="&exp_name";
    do i= 'aa','ab', 'ac', 'ea','ec';
        do j='01','02';
            do k=101 to 210;
                bee_created=compress (i||j||'-'||put(k, best.));
                rename i=group_id;
                output;
            end;
        end;
    end;
run;

/*dataset 2*/
data tag_num_replicate04a;
    length i $5. bee_created $9.;
    exp_name="&exp_name";
    do i= 'aa','ab', 'ac';
        do j='04';
            do k=501 to 615;
                bee_created=compress (i||j||'-'||put(k, best.));
                rename i=group_id;
                output;
            end;
        end;
    end;
run;

/*dataset 3*/
data tag_num_replicate04b;
    length i $5. bee_created $9.;
    exp_name="&exp_name";
    do i= 'ea';
        do j='04';
            do k=501 to 623;
                bee_created=compress (i||j||'-'||put(k, best.));
                rename i=group_id;
                output;
            end;
        end;
    end;
run;

/*dataset 4*/
data tag_num_replicate04c;
    length i $5. bee_created $9.;
    exp_name="&exp_name";
    do i= 'ec';
        do j='04';
            do k=501 to 620;
                bee_created=compress (i||j||'-'||put(k, best.));
                rename i=group_id;
                output;
            end;
        end;
    end;
run;

I tried to generate a list of 'aa', 'ab' and 'ac' but ended up creating four variables i, aa, ab and ac. I would highly appreciate if you could also show me what is wrong in the following codes.

/*macro not working*/
%macro tag_generate(groups=); 
data tag_num_test;
    exp_name="&exp_name";
        %do i= 1 %to 3; 
            j=%scan(&groups, &i);
            output;
        %end;
run;
%mend tag_generate;
%tag_generate(groups= aa ab ac);

Chang

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
Chang
  • 11
  • 1
  • 5
  • Since there is no overall logic in what i, j and k are for each dataset. You could turn those ranges into additional parameters for your macro i suppose. – mvherweg Aug 04 '13 at 14:55
  • Read [this paper](http://www2.sas.com/proceedings/forum2007/183-2007.pdf). Then read it again. – Joe Aug 05 '13 at 04:09

1 Answers1

0

I don't think you need a macro do loop or macro scan - you could just use a data step do loop and the regular scan function.

%macro tag_generate(i, j, krange, rep, exp);
data tag_num_replicate&rep;
   length i $5. j $5.;
   exp_name=&exp;
   do inum = 1 to count(&i, ' ')+1;
      i = scan(&i, inum);
      do jnum = 1 to count(&j, ' ')+1;
         j = scan(&j, jnum);
         do k = &krange;
            bee_created=compress (i||j||'-'||strip(k));
            rename i=group_id;
            drop inum jnum;
            output;
         end;
      end;
   end;
run;
%mend tag_generate;
%tag_generate("aa ab ac ea ec", "01 02", 101 to 202, 01_02, "expname1");

Edit: Tested and fixed. It should work now.

catquas
  • 712
  • 1
  • 5
  • 7