0
votes

I just learned about the "do" loop today and would like to try using it for data entry in SAS. I have tried most examples online, but I still cannot figure it out.

My dataset in an experiment with 6 treatments (1 to 6) using 2 sets of cues, 3 each, Visual and Audio. There's lag measured in seconds, which are 5, 10, and 15, which there are 2 sets. Basically it looks like this: Table

The entries I want are: 1. Obs_no, ranging from 1 to 18 (total of 18 observations, this allows me to easily delete outliers with an IF THEN) 2. Treatment type, which are Auditory and Visual. 3.Treatment number, 1 to 6, 3 sets. 4. Lag, 5, 10 or 15. 5. And the data itself

So far, my code makes 2 and 5 possible, it also makes the rest possible with an IF THEN statement and input statement, although I assume there's a way easier method:

data AVCue; 
do cue = 'Auditory','Visual';
    do i = 1 to 3;
        input AVCue @@;
        output;
        end;
    end;
datalines;
.204 .167 .202 .257 .283 .256 
.170 .182 .198 .279 .235 .281
.181 .187 .236 .269 .260 .258
;

Lag and the rest was made possible using an IF THEN statement and the crude method of input:

data AVCue;
set AVCue;
IF i=1 THEN Lag=5;
IF i=2 THEN Lag=10;
IF i=3 THEN Lag=15;
input obs_no treatment;
cards;
1 1
2 2
3 3
4 4
5 5
6 6
7 1
8 2
9 3
10 4
11 5
12 6
13 1
14 2
15 3
16 4
17 5
18 6
;
proc print data=AVCue;
run;

The IF THEN should be fine, but the input statement here is just in my opinion counterproductive, and defeats the purpose of using loops, which is to me, to save time. If done this way, I might as well just put the data into excel and import it, or type everything out with ample copy and paste of the text in the

input obs_no treatment;
cards;

section.

My coding knowledge is basic, so sorry if this question sounds silly, I want to know: 1. How would I make a list of numbers using the "do" loops in SAS? I've made several attempts and all I get is a list containing the next number. I know why this happens, the loop counts to x and the value assigned would just be x. I just don't know how to get around that. Somehow this didn't happen in the datalines section, I guess SAS knows there's 18 numbers and the entry i is stored accordingly... or something? 2. How would I go about assigning in this case, the numbers 1 to 6 to each entry?

Thanks!

1
You only have half the values you need for your pattern. 2*6*3 = 36, not 18. - Tom

1 Answers

1
votes

It is certainly much easier to read in the actual dataset instead of having to impute some of the variables based on the order the values have in the source data. You might be able to combine a SET statement and an INPUT statement in the same data step and get it to work, but it is probably NOT worth the effort. Just make two datasets and merge them.

Looking at the photograph you posted it looks like TREATMENT is not an independent variable. Instead it is just a label for the combination of CUE and LAG. To make it cycle from 1 to 6 just reset it back to 1 when it gets too large.

data AVCue; 
  do cue = 'Auditory','Visual';
    do lag= 5, 10, 15 ;
        treatment+1;
        if treatment=7 then treatment=1;
        obsno+1;
        input AVCue @@;
        output;
    end;
  end;
datalines;
.204 .167 .202 .257 .283 .256 
.170 .182 .198 .279 .235 .281
.181 .187 .236 .269 .260 .258
;

You can get in trouble if you just let SAS guess at how you want to define your variables. For example if you change the order of the CUE values do cue = 'Visual','Auditory'; then SAS will make CUE with length $5 instead of $8. Add a LENGTH statement to define your variables before you use them.

length obsno 8 treatment 8 cue $8 lag 8 AVCue 8 ;

This will also let you control the order they are created in the dataset.

enter image description here

If you really did already have a SAS dataset and you wanted to add a variable like TREATMENT that cycled from 1 to 6 (or really any DO loop construct) then could nest the SET statement inside the DO loop. Just remember to add the explicit OUTPUT statement.

data new ;
  do treatment=1 to 6 ;
    set old;
    output;
  end;
run;