BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
savanahb
Obsidian | Level 7

I had this assignment last week for an intro to SAS class that I am taking involving and we are working with arrays. I was supposed to present this data in a chart as described in the excel sheet I am attaching. I have managed to get everything to work except for labeling the agegroups (1-4) with different names (>30, 31-45, 46-60, 60+). It was suggested to continue using the else-if statements or to use proc format. Below is what I have, but it is just creating a new variable 'i'. Does anyone have any suggestions?

data HW4_F17; 
length Profession $ 9 Housingtype $ 11 tenure $ 5 Response $ 3;
array c condo1-condo4;
array t town1 - town4;
do profession = 'Physician','Professor','Engineer';
   do tenure ='Rent','Own','Lease';
      do response='Yes','No';
         input condo1-condo4 Town1-Town4;
         do agegroup=1 to 4;
            Housingtype='Condominium';
            Number = c[agegroup];
            output;
         end;
         do agegroup=1 to 4;
            Housingtype='Townhomes';
            Number = t[agegroup];
            output;
         end;
		 do agegroup=1 to 4;
		 	if i=1 then agegroup='>30';
			else if i=2 then agegroup='31-45';
			else if i=3 then agegroup='46-60';
			else if i=4 then agegroup='60+';
		end;
      end;
   end;
end;
drop condo: town:;		
datalines;
18 15 6 8 34 10 2 3
15 13 9 10 28 4 6 9
5 3 1 2 56 56 35 45
1 1 1 3 12 21 8 15
16 18 6 17 37 11 3 22
12 10 3 9 23 9 1 14
17 10 15 13 29 3 7 8
34 17 19 23 44 13 16 15
2 0 3 1 23 52 49 25
3 2 0 2 9 31 51 19
15 18 6 8 32 11 3 7
14 9 3 10 23 11 1 5
30 23 21 18 22 13 11 10
25 19 40 30 25 16 12 16
8 5 1 6 54 191 102 95
4 2 2 3 19 76 61 54
10 21 8 11 29 10 4 11
11 9 4 7 21 9 2 13
;	

proc print data=HW4_F17;
	title 'Tabulation of data from HW4_F17 data set';
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

I would tell the instructor that there is no need for any ARRAY statements to perform that task.

data HW4_F17; 
length Profession Housingtype Tenure Response AgeGroup $20 Number 8;
do profession = 'Physician','Professor','Engineer';
  do tenure ='Rent','Own','Lease';
    do response='Yes','No';
      do Housingtype='Condominium','Townhomes';
        do agegroup='<=30','31-45','46-60','60+';
          input number @@ ;
          output;
        end;
      end;
    end;
  end;
end;

View solution in original post

3 REPLIES 3
Astounding
PROC Star

Very few posters will risk opening a spreadsheet.  So mostly you will be getting "blind" answers to your questions, if any.

 

Note that creating a new variable is the whole idea of IF/THEN statements.  If that is the result you are obtaining, and if the values of the new variable are correct, your work is done.  I leave it up to you to determine whether you have obtained the proper values, but will note one piece that needs to be changed.

 

Your program defines AGEGROUP as a numeric variable, because this statement is the first one to mention AGEGROUP:

 

do agegroup=1 to 4;

 

As a result, you cannot assign character strings to AGEGROUP.  This statement would generate a note on the log:

 

if i=1 then agegroup='> 30' ;

 

Always read the log.  You cannot afford to ignore such notes.

 

There are several other issues that appear suspicious, such as nesting the INPUT statement inside DO loops, and outputting before all the IF/THEN statements have been applied.  To get more help, you will probably need to spell out what the results should look like for your first line of data.  Nothing helps like knowing where you are heading.

savanahb
Obsidian | Level 7

@Astounding Thank you for that information. I really appreciate it. As a new user, and teaching myself, I am still very much trying to figure out how all of this works. Also, thank you for the heads up about the excel sheet thing. 🙂

 

Tom
Super User Tom
Super User

I would tell the instructor that there is no need for any ARRAY statements to perform that task.

data HW4_F17; 
length Profession Housingtype Tenure Response AgeGroup $20 Number 8;
do profession = 'Physician','Professor','Engineer';
  do tenure ='Rent','Own','Lease';
    do response='Yes','No';
      do Housingtype='Condominium','Townhomes';
        do agegroup='<=30','31-45','46-60','60+';
          input number @@ ;
          output;
        end;
      end;
    end;
  end;
end;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1253 views
  • 2 likes
  • 3 in conversation