/* If-then statements and data merging. Anne Fairlie. October 2, 2008. For Graduate Lab Course*/ /*website to retrieve data file 'http://www.uri.edu/faculty/walls/solomon1.csv' */ /*second try--reading in as comma delimited with input of real col names*/ filename sol url 'http://www.uri.edu/faculty/walls/solomon1.csv' /*user='Ted' /*prompt proxy='http://inetgw.unx.sas.com:80'*/; data temp1; infile sol dlm=','; input ID warmcold convers generous happy goodnat humor sociable popular; firstobs = 2; run; proc contents data = temp1; run; data temp2; set temp1; if id = . then delete; proc print data = temp2; run; proc univariate data=temp2; var convers; run; /*reading data in directly*/ data made_from_log_file; infile datalines delimiter=','; input ID warmcold convers generous happy goodnat humor sociable popular; datalines; 8,0,3,5,4,5,6,5,5 9,0,2,4,4,4,5,5,5 10,0,2,5,6,5,5,3,3 11,0,3,5,3,2,2,4,3 12,0,3,3,3,2,5,4,4 13,0,3,4,4,5,5,5,5 14,0,2,6,5,4,6,6,6 15,0,2,4,5,5,5,5,5 17,0,3,3,3,1,3,5,5 18,0,4,3,3,3,2,3,4 21,0,3,4,4,3,5,6,4 23,0,4,4,3,4,4,3,4 24,0,2,3,6,5,5,5,4 27,0,4,3,3,5,4,4,4 28,0,2,1,2,2,5,4,3 31,0,3,4,4,4,4,3,3 34,0,4,3,5,5,3,6,6 35,0,4,3,5,5,3,6,6 36,0,4,3,5,2,2,2,4 38,0,4,5,3,3,3,4,4 1,1,4,2,3,3,5,4,4 2,1,4,2,2,2,3,1,1 3,1,4,3,1,1,3,2,3 4,1,1,2,4,4,6,5,4 5,1,5,3,3,2,4,2,3 6,1,2,4,4,3,4,3,3 7,1,2,3,2,3,4,4,5 19,1,3,1,2,2,2,2,1 20,1,6,2,2,2,4,3,2 22,1,4,1,2,2,3,1,1 25,1,2,2,1,1,4,1,2 26,1,5,2,2,2,4,2,2 29,1,4,3,2,1,3,2,2 30,1,4,4,4,2,1,2,1 32,1,4,1,3,2,3,2,3 33,1,4,2,3,2,3,2,3 37,1,4,2,2,2,3,3,3 run; proc print data = made_from_log_file; run; proc univariate data=made_from_log_file; var convers; run; data if_when; set made_from_log_file; x = ranuni(0); /*assigns a value from 0 or 1 to each subject*/ run; proc print data = if_when; var x; run; Data ifdrop; set if_when; if x > .2 then delete; /*DROPS some WHOLE ROWS of data; I got ##--you will get more or less--why?*/ run; Data ifkeep; set if_when; if x > .2; /*Alternative - KEEPS only observations specified*/ run; Data if1; set if_when; if x > .2 then convers = .; /*recoding responses to missing for demonstration purposes*/ run; proc print data = if1; var x convers; run; proc means data = if_when; var convers; run; /*record this mean*/ proc means data = if1; var convers; run; data if2; set if1; if convers = . then convers = 3.32; /*example of 'mean substitution (bad idea) and recoding into the same variable with an if then conditional statement*/ /*result--usually retains approximate value of the mean but biases the variance---How?*/ run; proc means data = if2; var convers; run; data if_else; set if_when; /*example of if--else*/ bobo = ' '; if x > .2 and x < .35 then bobo = 'doll'; else bobo = 'not a doll'; bobo1 = .; if x > .2 and x < .35 then bobo1 = 36; else bobo1 = 200; green = .; if x > .2 and x < .35 then green = 1; else if x > .45 then green = 2; run; proc print data = if_else; var x bobo bobo1 green; run; /*Alternative symbols to use in 'if-then' conditional statements = NE GT or > GE LT or < LE end of list*/ /*Example using two different variables in the if statement*/ data if_else; set if_when; smile = .; if happy LE 3 AND humor LE 3 then smile = 0; else if happy GE 4 AND humor GE 4 then smile = 1; run; proc print data = if_else; var happy humor smile; run; /*IN operator */ data if_else; set if_when; glee = .; if happy in (3,4,5) then glee = 17; else glee = 1; run; proc print data = if_else; var happy glee; run; /*NOTIN operator */ data if_else; set if_when; laugh = .; if humor notin (2,4) then laugh = 17; run; proc print data = if_else; var humor laugh; run; /* DATA MERGING */ /*In order to concatenate 2 data sets you need to be sure that both data sets are active could be used to add participants to an existing data file*/ data filea; input id var1; cards; 1 10 2 20 3 30 4 40 5 50 ; run; data fileb; input id var1; cards; 6 60 7 70 8 80 9 90 10 100 ; run; data fileab; set filea fileb; proc print data=fileab; run; /*Merging 2 data sets could be used to add variables to participants' data*/ data filec; input id var1; cards; 1 10 2 20 3 30 4 40 5 50 ; proc sort data=filec; by id; run; data filed; input id var2; cards; 1 60 2 70 4 90 5 100 3 80 ; proc sort data=filed; by id; run; data filecd; merge filec filed; by id; run; proc print data=filecd; run; /* proc format and proc freq*/ proc format; value warmcold 0 = ‘warm’ 1 = ‘cold’ ; run; proc freq data = if_when; format warmcold warmcold.; table warmcold; run; proc contents; run;