## Arrays and Do loops

Solved
Occasional Contributor
Posts: 16

# Arrays and Do loops

I'm a student and having trouble with some homework. I have this data set and using arrays and looping, i have to create two new variables for 1.) the change in weight (in kilograms) and 2.) the change in blood pressure for subjects. I've already used an array to convert the given weight in pounds to kilograms(kg1). The data is given below.

SAS Output

Obs id drug sbp weight kg1 change_kg i 1 2 3 4 5 6 7 8 9 10
 1 Placebo 162.953 299.211 135.697 . 1 1 Placebo 152.081 278.111 126.127 . 1 2 Placebo 173.683 330.057 149.686 . 1 2 Placebo 151.67 303.275 137.54 . 1 3 Placebo 163.512 304.551 138.118 . 1 3 Placebo 152.773 290.471 131.733 . 1 4 Placebo 169.091 343.915 155.971 . 1 4 Placebo 141.962 323.345 146.642 . 1 5 Placebo 169.663 268.525 121.78 . 1 5 Placebo 143.971 230.738 104.643 . 1

Accepted Solutions
Solution
‎09-25-2017 11:40 AM
Occasional Contributor
Posts: 16

## Re: Arrays and Do loops

Thank you everyone for your responses. It brought to my attention that my data could not be read and solved by the way my data was being read. So I combined the lines 1 and 2 of each subject patient and from there creating an array was easy. As others have said, creating an array to solve the differences of weight and systolic blood pressure is not needed. Therefore, i created a new variable using the variables i created with the array. Here is the code i created to solve my answer:

filename array1 'C:\Users\oscar\Documents\Fall 2017\BS805\hw2\hw2_f2017_blood.txt';
proc format;
value drugf
0 = 'Placebo'
1 = 'Study Drug';
value compliancef
0 = 'Not Compliant'
1 = 'Compliant';

libname hw2_perm 'C:\Users\oscar\Documents\Fall 2017\BS805\hw2';

data hw2_perm.arrays;
infile array1;
input #1 id 1-2 drug 3 sbp1 4-10 weight1 11-17
#2 sbp2 4-10 weight2 11-17 @;

format drug drugf.;
array kg(2) weight1 weight2;
array wkg(2) wkg1 wkg2;
do a=1 to 2;
wkg(a) = kg(a)/2.205;
end;
drop a;
diff_wkg = wkg1 - wkg2;
diff_spb = sbp1 - sbp2;
if diff_wkg >= 10 then compliance = 1;
if diff_wkg < 10 then compliance = 0;
format compliance compliancef.;
run;
proc print data = hw2_perm.arrays;
run;

The bolded section is what really helped me solve this problem. Thank you again everyone

All Replies
Super Contributor
Posts: 368

## Re: Arrays and Do loops

data one;

length Obsid 8 drug \$10 sbp weight kg1 change_kg i 8;

input Obsid drug sbp weight kg1 change_kg i;

datalines;

1 Placebo 162.953 299.211 135.697 . 1

1 Placebo 152.081 278.111 126.127 . 1

2 Placebo 173.683 330.057 149.686 . 1

2 Placebo 151.670 303.275 137.540 . 1

3 Placebo 163.512 304.551 138.118 . 1

3 Placebo 152.773 290.471 131.733 . 1

4 Placebo 169.091 343.915 155.971 . 1

4 Placebo 141.962 323.345 146.642 . 1

5 Placebo 169.663 268.525 121.780 . 1

5 Placebo 143.971 230.738 104.643 . 1

;

run;

data two;

set one;

p_sbp = lag(sbp);

p_wkg = lag(kg1);

c_sbp = sum(sbp,-p_sbp);

c_wkg = sum(kg1,-p_wkg);

run;

//Fredrik

Super Contributor
Posts: 319

## Re: Arrays and Do loops

The forum shall not be a place to solve homeworks, at least when the OP does not even provide his/her version of coding.

I don't believe this is the path to learning SAS or any other software.

Super Contributor
Posts: 368

## Re: Arrays and Do loops

I'm not shure I fully agree, sometimes a little push (cheers, tip, example, guideline) can help you take big steps forward.

If someone (in this case me) is willing to spend some of their time helping students with their home work, why not?

//Fredrik

Occasional Contributor
Posts: 16

## Re: Arrays and Do loops

For those who don't want to help thats fine. I have spent numerous hours trying to solve this and would rather get some sort of direction on how I can approach this problem than continue aimlessly without any progress. Not asking for a handout. Anyway, here is my current code (sorry i forgot to add this):

data hw2_perm.arrays;
infile array1;
input id 1-2 drug 3 sbp 4-10 weight 11-17;
format drug drugf.;
array lbs{1} weight;
array kg{1} kilograms;
array post{1} difference_kg;
do i = 1;
kg{i} = lbs{i}/2.205;
end;
drop i;
run;

Super User
Posts: 6,632

## Re: Arrays and Do loops

[ Edited ]

I agree with both of you.  I'm willing to help students when they put forth the effort, and it almost always helps to see what the student has done so far.  In this particular case, @ogarduno you need to post your code for a different reason.  It's almost impossible to conceive how using arrays and DO loops would help you convert WEIGHT from one scale to another.  So even if you are getting the correct answer for that, it is likely you are complicating the code unnecessarily.

Also not stated ... when you want to compute the difference in WEIGHT or SBP, what should the difference be when you come to the first observation for a new person?

***********************

EDITED:  Looks like we were both composing our posts at the same time.  You don't need any of the ARRAY statements.  To work with a single variable, you could simply use:

kilograms = weight / 2.205;

Arrays would become useful if you had 5 different weight measurements (all on the same observation), and wanted to convert all 5 of them using the same formula.

Occasional Contributor
Posts: 16

## Re: Arrays and Do loops

I guess I should explain more, and I apologize, but in the original post take note that each subject patient has 2 lines. The first line is pre-vitals and weight before taking an experimental drug or placebo. The second line is after taking this drug or placebo for certain time period. What I need to find out is the difference between that first line and second line's weight variable. I only put down the first 10 lines because the information actually has 140 lines of patient subject info and I didn't want to put all of that in here. The question specifically asks for me to use an array and do loop to solve this question. I'm stuck on how an array can read two lines instead of one. This is the second week of class so I don't think it should be that complicated of answer so I might be overthinking on how to solve this

Super User
Posts: 9,890

## Re: Arrays and Do loops

An array does not solve an issue where data needs to be compared across lines, as an array only has access to the data currently present in the PDV. To access data in a previously read observation, you can use the lag() function or a RETAINed variable.

To make your issue clear to us, you should first post a sample of your data as is, so we can see the real structure. The data in your initial post makes no sense, as there cannot be variables in SAS named 1, 2, 3 and so on. Just enough observations to illustrate the issue.

Use a data step, as in the example given by @FredrikE

A tabular example of the expected outcome will also be helpful, of course.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 6,632

## Re: Arrays and Do loops

Truthfully, arrays are not the easiest place to start.  Arrays typically come in handy when processing many variables in the same fashion.  So they would not be helpful for reading two lines of data.  Rather, they would be helpful for processing both weight and SBP.  There, you are trying to get the difference for both variables.

To apply that to what you have now:

data hw2_perm.arrays;
infile array1;
input id 1-2 drug 3 sbp 4-10 weight 11-17;

kilograms = weight / 2.205;
array current {3} id kilograms sbp;
array differ    {3} difference_id difference_kilograms difference_sbp;
do i = 1 to 3;
differ{i} = dif(current{i});

if i > 1 and difference_id ne 0 then differ{i}=.;
end;
drop i difference_id;
run;

This approach goes through the extra trouble of checking for changes in ID, and wiping out any "difference" variables when beginning a new ID.

Super User
Posts: 23,296

## Re: Arrays and Do loops

@ogarduno In most languages an array/matrix process is what you need. SAS doesn't process data in that manner which is what gives it some of its power. SAS processes data line by line. Each line is considered independent. There are ways to look at the previous row.

Solution
‎09-25-2017 11:40 AM
Occasional Contributor
Posts: 16

## Re: Arrays and Do loops

Thank you everyone for your responses. It brought to my attention that my data could not be read and solved by the way my data was being read. So I combined the lines 1 and 2 of each subject patient and from there creating an array was easy. As others have said, creating an array to solve the differences of weight and systolic blood pressure is not needed. Therefore, i created a new variable using the variables i created with the array. Here is the code i created to solve my answer:

filename array1 'C:\Users\oscar\Documents\Fall 2017\BS805\hw2\hw2_f2017_blood.txt';
proc format;
value drugf
0 = 'Placebo'
1 = 'Study Drug';
value compliancef
0 = 'Not Compliant'
1 = 'Compliant';

libname hw2_perm 'C:\Users\oscar\Documents\Fall 2017\BS805\hw2';

data hw2_perm.arrays;
infile array1;
input #1 id 1-2 drug 3 sbp1 4-10 weight1 11-17
#2 sbp2 4-10 weight2 11-17 @;

format drug drugf.;
array kg(2) weight1 weight2;
array wkg(2) wkg1 wkg2;
do a=1 to 2;
wkg(a) = kg(a)/2.205;
end;
drop a;
diff_wkg = wkg1 - wkg2;
diff_spb = sbp1 - sbp2;
if diff_wkg >= 10 then compliance = 1;
if diff_wkg < 10 then compliance = 0;
format compliance compliancef.;
run;
proc print data = hw2_perm.arrays;
run;

The bolded section is what really helped me solve this problem. Thank you again everyone

Super User
Posts: 23,296

## Re: Arrays and Do loops

It brought to my attention that my data could not be read and solved by the way my data was being read.

That's not correct. There are other, easier ways to accomplish this without using arrays, so either this is a very bad question from your professor or you're not interpreting something correctly. If I had assigned this question and said use an array, the approach you used isn't likely the one I'd expect so I'm still not certain it's the one your professor is expecting. I would highly suggest talking to your professor to get clarification, which they're usually happy to help with. Or to understand that the question is ambiguous. Or it's possible that the approach was to learn how to transpose the data - which could be done before or after it was read.

Either way, saying it couldn't be accomplished in that manner is incorrect. I usually wouldn't bother pointing this out but in programming its very easy to get into mentalities of somethings not possible, when it actually means, " I don't know how to do it that way" and that causes all sorts of issues. Clarity and terminology is very important, in both how you answer and ask questions.

Good Luck.

Occasional Contributor
Posts: 16

## Re: Arrays and Do loops

I understand where you are coming from, but the data that we used for today's hw is based on a raw data file. Last week, we learned to structure raw data with infile and input with various input tools. Hence, the reason i used @ and #N in my input file. This class is regarded as the most difficult in our program due to not just learning our current lecture's material but being able to use it with previous material without any aid. It is strained that we understand each concept and when to use it without aid. I had a long summer so the beginning weeks will contain a lot of review for me.

☑ This topic is solved.