BookmarkSubscribeRSS Feed
ManitobaMoose
Quartz | Level 8

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 ;

6 REPLIES 6
Astounding
PROC Star

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?

Shmuel
Garnet | Level 18

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

       

ManitobaMoose
Quartz | Level 8

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 ;

Shmuel
Garnet | Level 18

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.

   

Reeza
Super User
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...

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 6 replies
  • 804 views
  • 7 likes
  • 5 in conversation