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 April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.