data main ;
input subid $ condition $ ;
cards ;
101 normal
101 abnormal
102 normal
102 abnormal
103 normal
103 abnormal
;
how to assign type variable
i want to data like see below
subid | condition | type |
101 | normal | 0 |
101 | abnormal | 1 |
102 | normal | 1 |
102 | abnormal | 0 |
103 | normal | 0 |
103 | abnormal | 1 |
Please explain the process that assigns a 0 or 1 to type.
@thanikondharish wrote:
subjects have type numbers like 0 means subject is not in that condition ,
1 means subject present in that condition
But this explains nothing. The question is not what does 0 or 1 mean. The question is: How do we assign the 0 or the 1?
Please post full information, how is the 0 or 1 assigned? Maybe:
x=rand('UNIFORM');
From the manual:
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a001466748.htm
Is type simply a random 0/1 variable?
If so then do
data want;
set main;
a=rand('Integer', 0, 1);
run;
is 0 and 1 always assigned to normal = 0 and abnormal = 1?
having a full understanding of what you have and what you want, and maybe the why helps others understand without having to solve the implied assumptions.
@VDD wrote:
is 0 and 1 always assigned to normal = 0 and abnormal = 1?
The original example did not follow this rule.
having a full understanding of what you have and what you want, and maybe the why helps others understand without having to solve the implied assumptions.
Yes, I agree completely, there's no way to answer the question without this explanation.
One way to assign a random value of 0 or 1.
data want; set have; type= rand('table',.5, .5) -1; run;
The .5 are the probabilities of selection. If you want/need other than 50/50 split adjust the values but keep a total of 1.
RAND() function with Bernoulli option and you can specify the probability of a 0 or 1.
@thanikondharish wrote:
data main ;
input subid $ condition $ ;
cards ;
101 normal
101 abnormal
102 normal
102 abnormal
103 normal
103 abnormal
;how to assign type variable
i want to data like see below
subid
condition
type
101
normal
0
101
abnormal
1
102
normal
1
102
abnormal
0
103
normal
0
103
abnormal
1
data main ;
input subid $ condition $ ;
array x{0:1} _temporary_ ;
call streaminit(12345678);
i=mod(_n_,2);
if i=1 then do;
if rand('bern',0.5)=1 then do;x{0}=1;x{1}=0;end;
else do;x{0}=0;x{1}=1;end;
end;
flag=x{i};
drop i ;
cards ;
101 normal
101 abnormal
102 normal
102 abnormal
103 normal
103 abnormal
;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.