Help using Base SAS procedures

How to make code more efficient, do I use an array and loop?

Reply
Occasional Contributor
Posts: 14

How to make code more efficient, do I use an array and loop?

I have the following code and was wondering if it would be possible to build thisinto an array with do loop so instead of writing code for each instance ofApplicantType the code can be built automatically depending on if the ifstatement is true. While at the moment I only have three applicanttypes in thefuture this will grow and I am trying to make the code more efficient. Any ideas?

if ApplicantType_1^="" then do;    

      /*M*/       Put "PES" @;

      /*M*/       put Type_1  $3. @;

                  put Status_1  $3. @;

                  put Code_1  $3. @;

                  put Indicator_1  $1. @;

                  put Fil_1  $3. @;

      end;

      if ApplicantType_2^="" then do;

      /*M*/       Put "PES" @;

      /*M*/       put Type_2  $3. @;

                  put Status_2  $3. @;

                  put Code_2  $3. @;

                  put Indicator_2  $1. @;

                  put Fil_2  $3. @;

      end;

      if ApplicantType_3^="" then do;

      /*M*/       Put "PES" @;

      /*M*/       put Type_3  $3. @;

                  put Status_3  $3. @;

                  put Code_3  $3. @;

                  put Indicator_3  $1. @;

                  put Fil_3  $3. @;

      end;

Trusted Advisor
Posts: 1,300

Re: How to make code more efficient, do I use an array and loop?


data foo;

input (applicanttype_1-applicanttype_3 type_1-type_3 status_1-status_3 code_1-code_3 indicator_1-indicator_3 fil_1-fil_3) ($);

cards;

D E F AAA BBB CCC AAA BBB CCC AAA BBB CCC A B C AAA BBB CCC

;

run;

%macro writeApplicants(nbr);

%do i=1 %to &nbr;

  if ^missing(ApplicantType_&i)

   then do;

    put "PES " (type_&i status_&i code_&i) (:$3.) indicator_&i :$1. fil_&i :$3. @;

   end;

%end;

%mend;

data _null_;

set foo;

%writeApplicants(3);

run;

PES AAA AAA AAA A AAA PES BBB BBB BBB B BBB PES CCC CCC CCC C CCC

Super User
Super User
Posts: 6,498

Re: How to make code more efficient, do I use an array and loop?

In the long run you might want to restructure your data to have mulitple observations instead of repeating all of those columns.

Otherwise this can be done with array, especially now that SAS allows array references in PUT statements.

array applicanttype_ (3) ;

array type_ (3) ;

array status_ (3) ;

array code_ (3);

array indicator_ (3) ;

array fil_ (3);

do i=1 to dim(fil_);

  if applicanttype_(i) ne ' ' then put

      "PES" (type_(i) status_(i) code_(i)) ($3.)

            indicator_(i) $1.  fil_(i) $3.

  ;

end;

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