SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Combining two numeric variables

Reply
Contributor
Posts: 34

Combining two numeric variables

Hi everyone,

I am trying to combine two variables about sexual orientation into one variable. These two variables are:

 

Variable 1: SEXUALORIENT1
Format:      -7 Refused to answer
                  -8 DK
                  -9 Not Ascertained
                  1 Gay
                  2 Straight
                  3 Bisexual
                  4 Something else
                  5 Respondent does not understand responses

Variable 2: SEXUALORIENT2
Format:    -1 = Inapplicable
                -9 Not Ascertained
                1 = Not straight but identify with other label
                2 = Transgender, transsexual, or gender variant
                3 = have not figured out your sexuality
                4 = Do now think of yourself as having sexua
                5 =  do not use labels to identify yourself
                6 = made a mistake and did not mean to pick
                7 = mean something else
 
In fact, variable 2 was asked only to those who answered "4 = Something else" to variable 1. I need to create one variable from these 2 variables that has three categories: (1) heterosexual/straight : variable 1 (code = 2), (2) LGBT: variable 1 (codes = 1 and 3) and variable 2 (codes = 1 and 2), (3) Unspecified (all other codes).
 
This is the SAS code I used but the number of those classified as LGBT in the combined variable is smaller than those in the separate variable and this does not make any sense:
 
DATA NEW1;
    SET NEW;
          IF SEXUALORIENT1 = 1 OR SEXUALORIENT1 = 3 THEN SEXORIEN1 = 10;
          IF SEXUALORIENT1 = 2 THEN SEXORIEN = 20;
          IF SEXUALORIENT1 = -7 OR SEXUALORIENT1 = -8 OR SEXUALORIENT1 = -9 OR SEXUALORIENT1 = 4 OR   
              SEXUALORIENT1 = 5 THEN SEXORIEN = 8;
          IF SEXUALORIENT2 = 1 OR SEXUALORIENT2 = 2 THEN SEXORIEN2 = 10;
          IF SEXUALORIENT2 = -1 OR SEXUALORIENT2 = -9 OR SEXUALORIENT2 = 3 OR
              SEXUALORIENT2 = 4 OR SEXUALORIENT2 = 5 OR SEXUALORIENT2 = 6 OR SEXUALORIENT2 =7 THEN SEXORIEN2 = 10;
          SEXORIEN = max(SEXORIEN1, SEXORIEN2);
RUN;
 
 
Thank you.
Super User
Posts: 11,343

Re: Combining two numeric variables

Posted in reply to sas_user4

I would start by replacing multiple OR statements for comparing one variable to multiple values with IN. That will make the code simpler to read and follow and you may see an omission or missassignment.

Example instead of:

SEXUALORIENT1 = -7 OR SEXUALORIENT1 = -8 OR SEXUALORIENT1 = -9 OR SEXUALORIENT1 = 4 OR   

              SEXUALORIENT1 = 5
 
use:
sexualorient1 in ( -7, -8, -9, 4, 5)
 
 
SEXORIEN = max(SEXORIEN1, SEXORIEN2); results in missing values when
SEXUALORIENT1 = 2 and
SEXUALORIENT2 in (1, 2) because  SEXORIEN1, SEXORIEN2 are not assigned in those 2 cases.
You may want
If missing(sexorien) then SEXORIEN = max(SEXORIEN1, SEXORIEN2);
 
Contributor
Posts: 34

Re: Combining two numeric variables

Posted in reply to sas_user4

Thanks a lot, Ballardw, for your reply.

This is the code I used:

 

DATA NEW3;
   SET NEW;
IF SEXUALORIENT1 in (1, 3) THEN SEXORIEN1 = 10;
IF SEXUALORIENT1 = 2  THEN SEXORIEN1 = 20;
IF SEXUALORIENT1 in ( -7, -8, -9, 4, 5) THEN SEXORIEN1 = 8;
IF SEXUALORIENT2 in (1, 2) THEN SEXORIEN2 = 10;
IF SEXUALORIENT2 in (-1, -9, 3, 4, 5, 6, 7) THEN SEXORIEN2 = 8;
If missing(SEXORIEN) then SEXORIEN = max(SEXORIEN1, SEXORIEN2);
RUN;
PROC SURVEYFREQ DATA=NEW3;
  WEIGHT WT_NATIONAL;
  STRATA STSTR;
TABLES CECIG SEXUALORIENT1 SEXUALORIENT2 SEXORIEN /CL;
RUN;
PROC SURVEYFREQ DATA=NEW3;
  WEIGHT WT_NATIONAL;
  STRATA STSTR;
TABLES CECIG*SEXORIEN /CL;
RUN;

 

Thank you!

Respected Advisor
Posts: 4,173

Re: Combining two numeric variables

Posted in reply to sas_user4

Probably use also an "ELSE" statement.

DATA NEW3;
  SET NEW;
  IF SEXUALORIENT1 in (1, 3) THEN SEXORIEN1 = 10;
  ELSE IF SEXUALORIENT1 = 2  THEN SEXORIEN1 = 20;
  ELSE IF SEXUALORIENT1 in ( -7, -8, -9, 4, 5) THEN SEXORIEN1 = 8;

  IF SEXUALORIENT2 in (1, 2) THEN SEXORIEN2 = 10;
  ELSE IF SEXUALORIENT2 in (-1, -9, 3, 4, 5, 6, 7) THEN SEXORIEN2 = 8;

  If missing(SEXORIEN) then SEXORIEN = max(SEXORIEN1, SEXORIEN2);
RUN;
Ask a Question
Discussion stats
  • 3 replies
  • 462 views
  • 1 like
  • 3 in conversation