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

Hello,

I bet this is easy but I've been failing for a while now so maybe someone can help me. Below is sample data.

 

ID H-Start H-End H_1 H_2 H_3 H_4 H_5 H_6 H_7 H_8 H_9 H_10 H_11 H_12
A 3 6 0 1 1 1 1 1 0 1 0 1 1 1
B 2 5 0 1 0 0 0 0 1 1 1 1 1 1
C 9 12 1 1 0 0 0 0 0 0 0 0 0

1

 

I need to sum the H_# for certain ranges.  The variable H-Start has the start H value and H-End has the end.  For ID  'A' I want to sum H_3 through H_6. I've been trying to write a do loop and macros to solve this but have been failing.  

I would like the output to look like this: 

ID H-Start H-End H_1 H_2 H_3 H_4 H_5 H_6 H_7 H_8 H_9 H_10 H_11 H_12 Want_Sum
1 3 6 0 1 1 1 1 1 0 1 0 1 1 1 4
2 2 5 0 1 0 0 0 0 1 1 1 1 1 1 1
3 9 12 1 1 0 0 0 0 0 0 0 0 0 1 1

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
data want;
set have;

array _h(*) h_1 - h_12;

total_sum = 0;

do i=h_start to H_end;
total_sum = sum(total_sum, _h(i));
end;

run;

View solution in original post

14 REPLIES 14
novinosrin
Tourmaline | Level 20

data have;

input ID $ H_Start    H_End H_1  H_2  H_3  H_4  H_5  H_6  H_7  H_8  H_9  H_10     H_11 H_12;

datalines;

A    3    6    0    1    1    1    1    1    0    1    0    1    1    1

B    2    5    0    1    0    0    0    0    1    1    1    1    1    1

C    9    12   1    1    0    0    0    0    0    0    0    0    0    1

;

 

data want;

set have;

if id='A' then sum=sum(of h_3--h_6);

run;

weweaw
Obsidian | Level 7
Hi,
Sorry, maybe I wasn't clear. I want to add a variable to sum the specific H_# for each person. My real data has about 30K ID's and 60 H values.
Thanks.
novinosrin
Tourmaline | Level 20

Please provide a sample of your output table. Thank you

weweaw
Obsidian | Level 7

I added my wanted output to the original post.

Astounding
PROC Star

With 60 H variables:

 

data want;

set have;

array h_ {60};

total_h = 0;

if (1 <= h_start <= 60) and (1 <= h_end <= 60) then do _n_=h_start to h_end;

   total_h + h_{_n_};

end;

run;

weweaw
Obsidian | Level 7
I'm getting the error message (biop_pr is my var name for start number and biop is my variable for the stop value).
NOTE: Character values have been converted to numeric values at the places given by:
(Line):(Column).
613:14
NOTE: Variable hmo_biop_pr is uninitialized.
NOTE: Variable hmo_biop is uninitialized.
Astounding
PROC Star

This error message says you got it wrong.  Your data set does not contain variables named HMO_BIOP_PR or HMO_BIOP.  Maybe you spelled the variable names wrong.  Or maybe you chose the wrong data set.  But those variables don't exist in the data set that you are using.

 

If possible, it would be more helpful if you were to post the log so we can see any errors in context.

weweaw
Obsidian | Level 7
Sorry I was using my ready data H_# is hmo_# and biop_pr is for the H-start and biop is for the H-stop.
Reeza
Super User
data want;
set have;

array _h(*) h_1 - h_12;

total_sum = 0;

do i=h_start to H_end;
total_sum = sum(total_sum, _h(i));
end;

run;
weweaw
Obsidian | Level 7

I'm confused. For every ID there is a different start and stop H variables. For "do i=h_start to H_end;" What do I put in the '_start' and the '_end'?

 

Edit again to add the post I was referring to. 

data want;
set have;

array _h(*) h_1 - h_12;

total_sum = 0;

do i=h_start to H_end;
total_sum = sum(total_sum, _h(i));
end;

run;

 

Edited to add:

For A I need it to sum H_3 throught H_6, for B I need it to add H_2 though H_5, and C would be H_9 through H_12.  

The variable H-Start is the number of H_# I want it to begin summing and the variable H-end has the H I want it to stop at.

Reeza
Super User

@weweaw wrote:
I'm confused. For every ID there is a different start and stop H variables. 

 

That's why you use the variable names.

 


@weweaw wrote:
. For "do i=h_start to H_end;" What do I put in the '_start' and the '_end'?

There are several solutions so we don' tknow which you're referring to. 

It's best if you post your full code and log if you're having issues. 

weweaw
Obsidian | Level 7

Ok, I see where I changed it wrong. No error but total_sum is 0 for everything now and the other new variable i has odd numbers that my data could not sum to. 

data t3;
set try1;

array _h(*) hmo_1 - hmo_60;

total_sum = 0;

do i=biop_pr to biop;
total_sum = sum(total_sum, _h(i));
end;

run;

Astounding
PROC Star

The variable i can be ignored.  It will always be equal to BIOP + 1.

 

There are a couple of primary possibilities to explain your results.  First and foremost, are the variable names HMO_1, HMO_2, etc. the proper names of existing variables in your data set?  (Is it possible they are spelled incorrectly and should really be H_1, H_2, etc.?)  And secondly, if you do have the proper names, are they always either 0 or a missing value for the relevant range of variables (from BIOP_PR to BIOP)?

Reeza
Super User

Ok...given that it works for the sample data it's a bit hard to say what's wrong with yours. 

 

1. Check correct variable mappings in all places

2. Check array definition - type out all H_ to test if necessary

 

data have;
input ID $ H_Start    H_End H_1  H_2  H_3  H_4  H_5  H_6  H_7  H_8  H_9  H_10     H_11 H_12;
datalines;
A    3    6    0    1    1    1    1    1    0    1    0    1    1    1
B    2    5    0    1    0    0    0    0    1    1    1    1    1    1
C    9    12   1    1    0    0    0    0    0    0    0    0    0    1
;

data want;
set have;

array _h(*) h_1 - h_12;

do i=h_start to h_end;
 total_sum = sum(total_sum, _h(i));
end;

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 14 replies
  • 8029 views
  • 5 likes
  • 4 in conversation