Solved
New Contributor
Posts: 3

# Problem creating new variables

I'm trying to create a few new variables for various substance use combinations but i noticed something went wrong when I checked them. The one that should have indicated they only used alcohol was active for people who reported they were using multiple substances as well. The coding was the following (1=no use).

data icsus12;

set icsus12;

if pmalch=1 and pmcig = 1 and pmcigar = 1 and pmnosmk = 1 and pmhooka = 1

and pmmarij=1 and pmcoke = 1 and pmhall = 1 and pmhero = 1 and pminhal = 1

and pmrx = 1 and pmprx = 1 and pmocc = 1 and pmmeth = 1 and pmother = 1

then nouse = 1;

else nouse = 0;

run;

data icsus12;

set icsus12;

if pmalch>1 and pmcig = 1 and pmcigar = 1 and pmnosmk = 1 and pmhooka = 1

and pmmarij=1 and pmcoke = 1 and pmhall = 1 and pmhero = 1 and pminhal = 1

and pmrx = 1 and pmprx = 1 and pmocc = 1 and pmmeth = 1 and pmother = 1

then alchonly = 1;

else alchonly = 0;

run;

data icsus12;

set icsus12;

if (pmcig>1 or pmcigar>1 or pmnosmk>1 or pmhooka>1) and(pmalch=1)

and (pmmarij=1 and pmcoke = 1 and pmhall = 1 and pmhero = 1 and pminhal = 1

and pmrx = 1 and pmprx = 1 and pmocc = 1 and pmmeth = 1 and pmother = 1)

then tobonly = 1;

else tobonly = 0;

run;

data icsus12;

set icsus12;

if (pmalch>1) and (pmcig>1 or pmcigar>1 or pmnosmk>1 or pmhooka>1)

and (pmmarij=1 and pmcoke = 1 and pmhall = 1 and pmhero = 1 and pminhal = 1

and pmrx = 1 and pmprx = 1 and pmocc = 1 and pmmeth = 1 and pmother = 1)

then alchtob = 1;

else alchtob = 0;

run;

data icsus12;

set icsus12;

if (pmalch=1) and (pmcig=1 and pmcigar=1 or pmnosmk=1 or pmhooka=1)

and (pmmarij>1 or pmcoke> 1 or pmhall>1 and pmhero>1 and pminhal>1

or pmrx> 1 or pmprx> 1 or pmocc> 1 or pmmeth> 1 or pmother> 1)

then drugonly = 1;

else drugonly = 0;

run;

data icsus12;

set icsus12;

if (pmalch=1) and (pmcig>1 or pmcigar>1 or pmnosmk>1 or pmhooka>1)

and (pmmarij>1 or pmcoke> 1 or pmhall>1 and pmhero>1 and pminhal>1

or pmrx> 1 or pmprx> 1 or pmocc> 1 or pmmeth> 1 or pmother> 1)

then drugtob = 1;

else drugtob = 0;

run;

data icsus12;

set icsus12;

if (pmalch>1) or (pmcig>1 or pmcigar>1 or pmnosmk>1 or pmhooka>1)

and (pmmarij>1 or pmcoke> 1 or pmhall>1 or pmhero>1 or pminhal>1

or pmrx> 1 or pmprx> 1 or pmocc> 1 or pmmeth> 1 or pmother> 1)

then polysub = 1;

else polysub = 0;

run;

Thank you all for any help you can give. I've tried to fix it a few different ways but it doesn't crea

Accepted Solutions
Solution
‎04-30-2015 11:02 PM
Contributor
Posts: 54

## Re: Problem creating new variables

There's a problem with how you're setting the polysub flag:

if (pmalch>1)

or (pmcig>1 or pmcigar>1 or pmnosmk>1 or pmhooka>1)

and (pmmarij>1 or pmcoke> 1 or pmhall>1 or pmhero>1 or pminhal>1 or pmrx> 1 or pmprx> 1 or pmocc> 1 or pmmeth> 1 or pmother> 1)

then polysub = 1;

This will set it to 1 whenever pmalch > 1 regardless of the other two conditions; or when the second and third conditions are both true regardless of the first condition.

But you really need to think about what PGStats points out as well.

All Replies
Super User
Posts: 13,528

## Re: Problem creating new variables

Which variable? We do not know 1) which variable BY NAME received the wrong code or 2) The values of any of the other variables BY NAME when the "wrong" code was received.

Do any of the base variables have missing values?

BTW All of the code really should be in one datastep.

Every use of

Data dataname;

set dataname;

is asking for a coding error to reset a variable in a manner that my make it very hard to determine where the reset occurred.

New Contributor
Posts: 3

## Re: Problem creating new variables

Alchonly was being set to 1 for entries that also had polysub set to 1 which shouldn't happen. I'm not certain where that error might be getting in. I tried including it all in the same datastep and it still occurred.

There are missing value for the base variables but it's not numeric. Just the period.

Contributor
Posts: 54

## Re: Problem creating new variables

The period is the numeric missing value.

Could you show us some sample data, please?

New Contributor
Posts: 3

## Re: Problem creating new variables

EDIT: I added a sample of the actual data set as well. It includes the segment that was having the conflicting variable assignments.

data subsample;

input pmalch pmcig pmcigar pmnosmk pmhooka pmmarij pmcoke pmhall pmhero pminhal pmrx pmprx pmocc pmmeth pmother;

cards;

1 5 2 3 5 . 2 2 4 3 1 1 . 4 3

3 . 2 1 4 5 5 . 2 1 3 1 1 2 .

. . . . . . . . . . . . . . .

2 2 5 4 4 3 2 1 1 5 1 4 2 5 3

5 1 2 3 5 4 5 5 2 1 1 . . 2 3

5 1 1 1 1 1 1 1 1 1 1 1 1 1 1

1 2 3 2 5 1 1 1 1 1 1 1 1 1 1

1 1 1 1 1 2 . 4 3 5 1 2 3 4 3

2 2 1 1 1 1 1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

;

run;

That should generate sample data that should have at least one example of each new category I was trying to create. That's how it's structured in the actual set

Esteemed Advisor
Posts: 5,526

## Re: Problem creating new variables

In numerical comparisons, SAS missing values are simply considered as minus infinity. So if you want missing values to be considered as 'no substance use' then your comparisons should be pmsubst <= 1 or pmsubst > 1. If you want missing values to be ignored in the tests and to generate missing values for some categories when information is missing you'll have to use more complicated tests.

PG

PG
Solution
‎04-30-2015 11:02 PM
Contributor
Posts: 54

## Re: Problem creating new variables

There's a problem with how you're setting the polysub flag:

if (pmalch>1)

or (pmcig>1 or pmcigar>1 or pmnosmk>1 or pmhooka>1)

and (pmmarij>1 or pmcoke> 1 or pmhall>1 or pmhero>1 or pminhal>1 or pmrx> 1 or pmprx> 1 or pmocc> 1 or pmmeth> 1 or pmother> 1)

then polysub = 1;

This will set it to 1 whenever pmalch > 1 regardless of the other two conditions; or when the second and third conditions are both true regardless of the first condition.

But you really need to think about what PGStats points out as well.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 6 replies
• 312 views
• 3 likes
• 4 in conversation