I am trying to create a variable based on two variables
elig1
elig2
if elig1 or elig2 = . then elig3 = .;
else if elig1 = 1 then elig3 = 1;
else if elig2= 1 then elig3= 2 ;
else if elig2 or elig1 = 1 then elig3 = 3;
elig1 and elig2 or both yes or no variables
i want to have elig3 if both elig1 and elig2 are yes(1) but having trouble as output for elig3 just includes elig2
thanks,
Hi @knargis160
If we take he following practical example, are the conditions correctly applied?
data have;
input elig1 elig2;
datalines;
. .
1 1
. 1
1 .
;
data want;
set have;
if elig2 = 1 and elig1 = 1 then elig3 = 3;
else if elig2 = 1 then elig3 = 2 ;
else if elig1 = 1 then elig3 = 1;
else if elig1=. or elig2 = . then elig3 = .;
run;
and statement does not seem to work
output is giving me missing values. I am thinking and statement does not seem to work.
Do you have any options
Hi @knargis160
Please provide datalines as well as the expected output.
-> the rules to code the new variable are not clear at all.
datalines
ptid elig1 elig2
1. 0 1
2. 1 0
3. 1 1
4. 1 0
so on
while coding elig 3
i am having # for elig 1 but everything else is missing
@knargis160 wrote:
datalines
ptid elig1 elig2
1. 0 1
2. 1 0
3. 1 1
4. 1 0
so on
while coding elig 3
i am having # for elig 1 but everything else is missing
So you gave us the inputs to your functions truth table, but you did not give the results you want. Add a column for what results you want.
ELIG1 ELIG2 WANT 0 0 ? 0 1 ? 1 0 ? 1 1 ?
If you want the treat missing values of ELIGx as different than 0 then you need 3x3=9 rows for the truth table instead of just 2x2=4 rows.
datalines;
ptid elig1 elig2
1. 0 1
2. 1 0
3. 1 1
4. 1 0
so on
I want to calculate a new variable elig 3 based on elig1 and elig2
output
ptid elig1 elig2 elig3
1. 0 1 1
2. 1 0 2
3. 1 1 3
4. 1 0 2
2. 1 0 2
3. 1 1 3
4. 1 0 1
5. 1 1 3
Please try this:
data want;
set have;
if elig2 = 1 and elig1 = 1 then elig3 = 3;
else if elig1 = 1 then elig3 = 2;
else if elig2 = 1 then elig3= 1 ;
else elig3 = .;
run;
I believe there is a mistake in your example for this record ?
ptid elig1 elig2 elig3
1. 0 1 1
2. 1 0 2
3. 1 1 3
4. 1 0 2
2. 1 0 2
3. 1 1 3
4. 1 0 1
5. 1 1 3
@knargis160 wrote:
datalines;
ptid elig1 elig2
1. 0 1
2. 1 0
3. 1 1
4. 1 0
so on
I want to calculate a new variable elig 3 based on elig1 and elig2
output
ptid elig1 elig2 elig3
1. 0 1 1
2. 1 0 2
3. 1 1 3
4. 1 0 2
2. 1 0 2
3. 1 1 3
4. 1 0 1
5. 1 1 3
Why not just
2*elig1 + elig2
What do you mean by this fragment?
elig1 or elig2 = .
Did you mean:
elig1 or (elig2 = . )
So either ELIG1 is true or ELIG2 is missing? If that is what you want then it would be clearer to write:
elig1 or missing(elig2)
If you meant this
(elig1 or elig2) = .
then it can never be true since the result of a boolean operation is never missing. It is either TRUE (1) or FALSE (0).
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.