DATA Step, Macro, Functions and more

A little help with Arrays

Reply
Frequent Contributor
Posts: 121

A little help with Arrays

Hi,

 

I need to specify when any one of the Questions is a 5 (the questions range from 1 to 5), using arrays. Unfortunately, the program I have written doesn't work. Perhaps someone can offer some assistance. Thanks.

 

----------------------------------

Libname Learn'/folders/myfolders/Learn' ;

Data Survey2 ;
    Set Learn.Survey2 ;
    Array QuesNum(*)  $ Ques1-Ques5 ;
    do i = 1 to dim(QuesNum) ;
    end ;
    MaxQues = Max(of QuesNum) ;
    If MaxQues = 5 then Any = 'Yes' ;
    Else Any = 'No' ;
run ;

Super User
Posts: 6,632

Re: A little help with Arrays

Posted in reply to ManitobaMoose

Questions for you to get started:

 

Are you certain that QUES1 through QUES5 are the names of the variables you will be working with?

 

Do you know whether they are character or numeric?

Trusted Advisor
Posts: 1,831

Re: A little help with Arrays

Posted in reply to ManitobaMoose

1)   The line  MaxQues = Max(of QuesNum) ; is erronous, as MAX function needs a variable as argument

      while QuesNum is an array name and not a avriable.

2) Why do you use the max function is not clear !

3) You closed the DO loop with END without having any code inside.

     I assume you want to check - either i=5  or  QuestNum(i)=5  and then

     assign the Any variable to YES or NO.

    That should be inside the do loop (i.e. between the DO statement and the END statement).

4) Your input is in one row of 5 variables:   Ques1 - Ques5.

     You defined only one new variable ANY.

      You need either define ANY1 - ANY5 as a new array and assign the YES/NO to ANYX(i)

      assuming Array anyx any1-any5;

       or  add OUTPUT; statment to result in 5 output rows

       

Frequent Contributor
Posts: 121

Re: A little help with Arrays

Ok, I figured it out. Thanks for your help.

 

Here is my solution:

 

Libname Learn'/folders/myfolders/Learn' ;

Data Survey2 ;
    Set Learn.Survey2 (rename=
                         (Ques1 = CharQues1
                         Ques2 = CharQues2
                         Ques3 = CharQues3
                         Ques4 = CharQues4
                         Ques5 = CharQues5)) ;
                         
    Q1 = Input(CharQues1, 8.) ;
    Q2 = Input(CharQues2, 8.) ;
    Q3 = Input(CharQues3, 8.) ;
    Q4 = Input(CharQues4, 8.) ;
    Q5 = Input(CharQues5, 8.) ;
    
    Array QuesNum(*)  $ Ques1-Ques5 ;
    do i = 1 to dim(QuesNum) ;
    end ;
    Array AnyX(*)  Q1-Q5 ;
    do i = 1 to dim(AnyX) ;
    If Max(of Q1-Q5) = 5 then Any5s = 'Yes' ;
    Else Any5s = 'No' ;
    end ;
    drop i CharQues1-CharQues5 Ques1-Ques5 ;
run ;

Trusted Advisor
Posts: 1,831

Re: A little help with Arrays

Posted in reply to ManitobaMoose

You can shorten your code to:

Libname Learn'/folders/myfolders/Learn' ;

Data Survey2 ;
    Set Learn.Survey2;
        retain Any5s 'No' ;                     
        length Q1-Q5 3  Any5s $3 ;
    
    Array QuesNum(*)  $ Ques1-Ques5 ;
    Array Qx(*)  Q1-Q5 ;
	
    do i = 1 to dim(QuesNum) ;
	 Qx(i) = input(QuesNum(i));      
	 if Qx(i) = 5 then Any5s = 'Yes' ;
/* next 2 lines are for debug only - check in the log */
Q = Qx(i);
PUT i= Q= Any5s= ; end ; drop i Ques1-Ques5 ; run ;

   or even shorter to:

Libname Learn'/folders/myfolders/Learn' ;

Data Survey2 ;
    Set Learn.Survey2;
length Q $8 Any5s $3 ; retain Any5s 'No ' ; Array QuesNum(*) $ Ques1-Ques5 ; do i = 1 to dim(QuesNum) ; if QuesNum(i) = '5' then Any5s = 'Yes' ;
/* next 2 lines are for debug only - check in the log */
Q = QuesNum(i);
PUT i= Q= Any5s= ;
end ;
drop i Ques1-Ques5 ;
run ;

run and compare the two solutions.

   

Super User
Posts: 23,296

Re: A little help with Arrays

Posted in reply to ManitobaMoose
MaxQues = Max(of QuesNum(*)) ;

 

You should also check the WHICHN function, and you don't necessarily even need an array declaration in this case.

 

Find5 = WHICHN(5, of ques1-ques5);
MaxQuest = max(of ques1-ques5);

 

Relevant section of the documentation that explains variable lists and all the variations, table at the bottom is handy:

http://documentation.sas.com/?docsetId=lrcon&docsetTarget=p0wphcpsfgx6o7n1sjtqzizp1n39.htm&docsetVer...

 

Super User
Super User
Posts: 9,416

Re: A little help with Arrays

Posted in reply to ManitobaMoose

Way too overthought that, you are simply searching for "5" in some variables so:

data survey2;
  set learn.survey2;
  any=ifc(index(cats(of ques:),"5"),"Yes","No");
run;

What this does is the cats puts all the variables together, index returns >0 if 5 is found, if that is true (any non zero is true) then use test Yes, otherwise use text no.

Ask a Question
Discussion stats
  • 6 replies
  • 157 views
  • 7 likes
  • 5 in conversation