BookmarkSubscribeRSS Feed
stephneepa
Calcite | Level 5

Hello,

 

I have a dataset that includes the frequency that participants did 94 different activities in the past 30 days (1 frequency variable for each activity, each on a 0-4 scale) as well as an additional 94 variables indicating where each activity took place (1=indoors, 2=outdoors, 3=NA if the activity was not done). I would like to aggregate across this information so that I have an aggregate indicator of how often outdoor activities were done, incorporating both the number of outdoor activities as well as how often each of these was done.

 

I am a little rusty on use of arrays. I tried to create arrays for the 94 frequency and 94 activity variables; my ultimate goal is to calculate a SUM of all thearrays, sum across columns frequency variables for which the participant indicated the activity was typically done outdoors (so, a sum function but including only those activities where the corresponding where variable = 2).

 

This is what I have for code below, but based on the aggregate value coming out, I don't think it is working correctly. Any ideas? Thank you!

 


data bl;
set go.bl;
*Create an array of all the WHERE variables;
array actwhere {94} Ac1WatchTVwhere Ac1WatchMovieHomewhere [etc... each of these is a variable that shows the frequency the participate did a given activity, e.g. the first one here where they typically watched TV 1=indoors, 2=outdoors, 3=NA; I'm not going to list out all 94 here because it makes this hard to read] 


*Create an array of all the FREQ variables;
array actfreq {94} Ac1WatchTVfreq Ac1WatchMovieHomefreq [etc... again listing all 94 of these variables, which indicate the frequency of each activity on the 0-4 scale] 

 

do i = 1 to 94;

if actwhere{i} = 2 then do; *Trying to isolate activities done outside here before summing;
act_outdoor_freq=sum(of actfreq{i}); *Trying to create a new aggregate variable that is the sum of the frequencies of all those activities done outside;
end;


end;
run;

2 REPLIES 2
PaigeMiller
Diamond | Level 26
act_outdoor_freq=0;
do i = 1 to 94;
    if actwhere{i} = 2 then act_outdoor_freq=sum(act_outdoor_freq,actfreq{i}); 
end;
--
Paige Miller
Reeza
Super User
FYI - if you restructure your data to a long format these types of questions are much easier to answer.

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!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 1555 views
  • 0 likes
  • 3 in conversation