-2

I'm trying to get this macro with a do loop to work. Seems straightforward by it doesn't populate the new fields with anything. Please any help is appreciated. An array doesn't seem work as my data is all in columns. Thanks!

%macro labs;
data lab1;
set non1;
by record_id;
%do i=1 %to 25;
length lab_spec_type_&i $25.;
%if lab_spec_site&i = '2' %then lab_spec_type_&i = 'matser';
%else %if lab_spec_site&i = '1' %then lab_spec_type_&i = 'matbl';
%end;
%mend;
%labs;
  • 1
    It looks like you want an IF/ELSE IF rather than %IF/%ELSE %IF. Why do you think arrays wouldn't work? Can you show the HAVE data and the WANT (for a few records, and a few variables?) If you have lab_spec_site1-lab_spec_site25 and want to created lab_spec_type_1-lab_spec_type_25, that sounds like an array problem to me. Rather than generating 25 IF statements. – Quentin Jun 30 '17 at 15:12
  • 1
    provide some sample data for dataset non1 and lab1 – DCR Jun 30 '17 at 15:35
  • 1
    What you show above is identical to the array implementation. If you mean `is all in columns` you mean each of the 25 lab-whatevers is a separate *row*, then that's very different and not related to the above code in any useful way. – Joe Jun 30 '17 at 15:57
  • @JLH That sort of comment isn't helpful, unfortunately. You should be specific as to why a question does not meet our standards if you feel it does not. – Joe Jun 30 '17 at 17:03

1 Answers1

1

You should use an array here, you gain no value from having macro's unless you're after job security. Though that may backfire.

data lab1;
    set non1;
    by record_id;
    array labType(25) $ lab_spec_type_1-lab_spec_type_25;
    array site(25) $ lab_spec_site1-lab_spec_site25;

    do i=1 to 25;
        if site(i) = '2' then
            labType(i) = 'matser';
        else if site(i) = '1' then
            labType(i) = 'matbl';
    end;
run;
Reeza
  • 20,510
  • 4
  • 21
  • 38
  • You should add a length specification to the labtype array, otherwise SAS will make these new variables as numeric. – Tom Jul 01 '17 at 15:28
  • I added $ to array definitions. Hopefully OP can fix it from there. Thanks for the catch @Tom. – Reeza Jul 01 '17 at 20:03