## Create a code from multiple variables

Solved
Regular Contributor
Posts: 150

# Create a code from multiple variables

I have a question about creating a new variable. I have multiple yes/no variables, what I want to do is create a "code" that is a combo of those variables so:

if var1=yes

and var2=no

and var3=yes

and var4=no

then code= 1010;

Any suggestions would be greatly appreciated.

Thanks!

Accepted Solutions
Solution
‎05-30-2013 02:48 PM
Posts: 3,167

## Re: Create a code from multiple variables

If var1-va4 only take yes/no values, then how about:

code=cats((var1='yes'),(var2='yes'),(var3='yes'),(var4='yes'));

Haikuo

All Replies
Solution
‎05-30-2013 02:48 PM
Posts: 3,167

## Re: Create a code from multiple variables

If var1-va4 only take yes/no values, then how about:

code=cats((var1='yes'),(var2='yes'),(var3='yes'),(var4='yes'));

Haikuo

Super User
Posts: 13,523

## Re: Create a code from multiple variables

Do any of the variables have missing values? If so, should they be treated differently than a "No" value?

I'm only asking because I have done similar and then realized that I had missing data and for that project missing should not be treated as "No".

Regular Contributor
Posts: 150

## Re: Create a code from multiple variables

How do I treat the missing values differently? Each variable being used has a different number of missing values based on skip patterns, etc...

Super User
Posts: 13,523

## Re: Create a code from multiple variables

For my project where I was building a similar composite we ended up deciding that just knowing a specific variable was missing. The resolution wa the composite was a string and the missing variables were coded with an underscore "_". I had a format designed to "parse" the coded string. If you needed specific reasons a variable was missing other charcters would work.

Super User
Posts: 8,089

## Re: Create a code from multiple variables

Easier if you code your original variables as numbers instead of strings. Then CATS() will work and generate string of 0's, 1's and dots (for the missing values).

proc format ;

invalue nn (upcase) 'YES'=1 'NO'=0 other=.;

run;

data xx;

input (n1-n4) (:nn3.);

flag=cats(of n1-n4);

put (_all_) (=);

cards;

yes no . yes

run;

n1=1 n2=0 n3=. n4=1 flag=10.1

🔒 This topic is solved and locked.