BookmarkSubscribeRSS Feed
robwork
Calcite | Level 5

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;

2 REPLIES 2
FriedEgg
SAS Employee


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

Tom
Super User Tom
Super User

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;

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
  • 2 replies
  • 677 views
  • 0 likes
  • 3 in conversation