Hi everyone,
I am trying to combine two variables about sexual orientation into one variable. These two variables are:
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
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!
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;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.