BookmarkSubscribeRSS Feed
xoxozav_1
Calcite | Level 5

Hi!

I need some advice on how to generate a new combined variable by using if and then statements. I have created separate variables using the if statement, and I have also used the proc format. But after that, I need to combine the variables. I have been trying to combine those new variables, in something like "if at least two of the three materials (wall, roof, and floor) were finished", but I couldn't. Could you guide me to know how to do it?

 

This is the SAS code with the variables that I have to combine (wall, roof, and floor).

 

Thank you for your help 

libname Assig2 "/folders/myfolders/study3";
data work.homework2;
set Assig2.homework2;
  
if  HV215 in (31,32,33,34,35,36)    then Roof= 1;
if  HV215 in (11,12,13)             then Roof= 2;
if  HV215 in (21,22,23,24,25)      then Roof= 3;
   
if  HV213 in (31,33,34,35)    then Floor= 1;
if  HV213 in (11,12)          then Floor= 2;
if  HV213 in (21,22)          then Floor= 3;

if  HV214 in (31,32,33,34,35,36)    then Wall= 1;
if  HV214 in (11,12,13)             then Wall= 2;
if  HV214 in (21,22,23,24,25,26)      then Wall= 3;
run;
proc format;
value Wall
1="Finished"
2= "Natural"
3= "Rudimentary";
run;
proc format;
value Roof
1="Finished"
2= "Natural"
3= "Rudimentary";
run;
proc format;
value Floor
1="Finished"
2= "Natural"
3= "Rudimentary";
run;
Proc freq data=work.homework2;
tables Wall;
format Wall Wall. ;
run;
Proc freq data=work.homework2;
tables Roof;
format Roof Roof. ;
run;
Proc freq data=work.homework2;
tables Floor;
format Floor Floor. ;
run;
2 REPLIES 2
ed_sas_member
Meteorite | Level 14

Hi @xoxozav_1 

 

Does this additional statement in the DATA step meet your expectations ?

if (Roof=1 and Floor=1) or (Roof=1 and Wall=1) or (Floor=1 and Wall=1) then flag_finished = 1;
Kurt_Bremser
Super User

Your three formats are identical, so one will suffice.

You can also greatly simplify your code by using an informat for the transformation, and combining all freq tables into one procedure call.

Your question can be solved with the help of the countc() function, applied to a string containing all three values:

proc format;
invalue instate
  31,32,33,34,35,36 = 1
  11,12,13 = 2
  21,22,23,24,25,26 = 3
;
value outstate
  1 = "Finished"
  2 = "Natural"
  3 = "Rudimentary"
;
run;

data homework2; /* create some fake data */
input HV215 HV213 HV214;
datalines;
31 31 11
21 22 23
;

data homework2_2;
set homework2;
Roof = input(HV215,instate.);
Floor = input(HV213,instate.);
Wall = input(HV214,instate.);
format Roof Floor Wall outstate.;
flag_finished = (countc(cats(Roof,Floor,Wall),'1') > 1);
run;

proc freq data=work.homework2_2;
tables Wall;
tables Roof;
tables Floor;
run;

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 413 views
  • 2 likes
  • 3 in conversation