DATA Step, Macro, Functions and more

Arrays if/else statement

Reply
Occasional Contributor
Posts: 6

Arrays if/else statement

[ Edited ]

I need to assign treatments according to a house id, so if the house id for a subject is the same as the previous one's house id the subject gets the same treatment. For example, if the initial treatment for for subject 1 is "A" and house id = 0241 and the second subject's house id is also 0241 then the second subject will also get treatment "A". The first subject of the study was assigned a treatment using randomization and the result was treatment "B". There are a total of 3 treatments involved and only the first initial treatment for the first subject is randomized. 

 

I have a data set with all of the house ids but need to assign treatments based on the condition mentioned above. I believe i can use the lag function to look at the previous house ids and create an array to assign the same treatments. A example of the data is shown below the variable "fac" and "rand" are used later for the study.  This is what I have so far:

 

fac   houseiq rand

1      0240      0.7

2      0241      0.5

3      0232      0.6

5      0111       0.6

 

data scoreprob;(data with houseids)
set scoreprob;
x=lag(houseid);
Treatment_SEL1="B";(Initial Treatment for the first subject)
run;
data a;
set scoreprob;
DO i = 1 to 2310;
array x{2310} Treatment_SEL1-Treatment_SEL2310;
if x{i} eq houseid{i} then do;
Treatment_SEL{i} eq Treatment_SEL{i}+1;
i++;
end;
run;

 

I am getting a bunch of errors when i run the code, please let me know any corrections!!

 

Thank you. 

 

Super User
Posts: 9,878

Re: Arrays if/else statement

You use array houseid without declaring it. From the previous data step, I infer you already have a variable houseid, so you must use a different name for the array.

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

Re: Arrays if/else statement


@azt5173 wrote:

I need to assign treatments according to a house id, so if the house id for a subject is the same as the previous one's house id the subject gets the same treatment. For example, if the initial treatment for for subject 1 is "A" and house id = 0241 and the second subject's house id is also 0241 then the second subject will also get treatment "A". The first subject of the study was assigned a treatment using randomization and the result was treatment "B".

 

I have a data set with all of the house ids but need to assign treatments based on the condition mentioned above. I believe i can use the lag function to look at the previous house ids and create an array to assign the same treatments. This is what I have so far:

 

data scoreprob;(data with houseids)
set scoreprob;
x=lag(houseid);
Treatment_SEL1="B";(Initial Treatment for the first subject)
run;
data a;
set scoreprob;
DO i = 1 to 2310;
array x{2310} Treatment_SEL1-Treatment_SEL2310;
if x{i} eq houseid{i} then do;
Treatment_SEL{i} eq Treatment_SEL{i}+1;
i++;
end;
run;

 

I am getting a bunch of errors when i run the code, please let me know any corrections!!

 

Thank you. 

 


When getting errors from code copy the code and error messages from your log and paste into a code box opened with the forum's {I} icon. The code box retain formatting that the main message windows removes making the SAS diagnostics that often accompany error messages to appear in the wrong place.

 

It would also help to post some example data and what the result my look like.

What are the acceptable values of your treatment variable? I only see "B" used.

Is the current order of the data critical? I would be tempted to sort by the house id variable and then use first. processing to randomly assign the "treatment".

Here is brief example. This treats the sex variable as your house id to assign  a random number to the records by sex.

proc sort data=sashelp.class
   out=work.class;
   by sex;
run;

data work.example;
   set work.class;
   by sex;
   retain treatment;
   if first.sex then treatment=rand('uniform');
run;

How many treatments are involved? The rand('table') function might be a good way to do this if you only have a few levels of treatment.

 

Occasional Contributor
Posts: 6

Re: Arrays if/else statement

Hi, I updated the question below. Thank you for your reply as your code should work only the first subject's treatment is being randomized and the other ones we have to use the house id to assign the initial treatments. 

Super User
Posts: 13,298

Re: Arrays if/else statement

Show some actual data. You haven't described or shown 1) how many different treatment values you have 2) what the input data set might look like (dummy data is fine as long as it shows examples of what the input data contains that are used in the process) and 3) what a plausible outcome could look like. Yes with random treatments we would not expect to get the same result but could show a method.

 


@azt5173 wrote:

Hi, I updated the question below. Thank you for your reply as your code should work only the first subject's treatment is being randomized and the other ones we have to use the house id to assign the initial treatments. 


What is rule involved? How do you use the "house id"? You imply some thing about when it changes but stop there. With an example we can make suggestions. But this is very incomplete description of what is needed to assign the treatment.

Here's a different example with similar data:

proc sort data=sashelp.class
   out=work.class;
   by age;
run;

data work.example;
   set work.class;
   by age;
   retain treatment;
   if first.age then treatment=rand('table',1/3,1/3,1/3);
run;

Each age gets assigned to one of three treatments. With only 6 ages in that example data not all three may get used. Re-run data step to see possible differences. The key is that when Age changes we get a different assignment possibility. Since this is random it might be the same as the previous or not.

 

If the rule is that the treatment must be different then you have to explicitly state that as a requirement. You need to describe all of the rules involved, all of the values of treatment possible and best is to provide example data.

Ask a Question
Discussion stats
  • 4 replies
  • 56 views
  • 0 likes
  • 3 in conversation