*Lookup table example; data one; input score age; array answer {13:15,1:5} _temporary_ (4 5 5 5 5 3 4 5 5 5 2 3 4 5 5); adjusted = answer(age,score); datalines; 5 13 5 15 4 13 5 14 1 13 2 15 1 14 3 15 4 14 3 15 ; run; proc print; run; *Lookup table with 3-dimensional array; data inputdata; input sex $ score age; *The last two rows should generate different adjusted scores; datalines; M 5 13.5 F 5 13.5 M 4 12.5 M 5 13.0 F 1 12.5 F 2 13.5 M 1 13.5 F 3 13.5 F 4 13.0 M 3 13.5 F 2 12.5 M 2 12.5 ; run; proc format; invalue gender 'F'=1 'M'=2; invalue age 12.5=1 13.0=2 13.5=3; run; data one; set inputdata; array answer {1:2,0:3,0:6} _temporary_ (1 1 2 3 4 5 . 1 4 4 5 5 5 12.5 1 3 4 4 5 5 13.0 1 2 3 4 4 5 13.5 2 1 2 3 4 5 . 2 4 5 5 5 5 12.5 2 3 4 5 5 5 13.0 2 2 3 4 5 5 13.5); adjusted=answer(input(sex,gender.), input(age,age.),score); proc print; run; *Stored lookup table; data lookup; input age adjscore1-adjscore5; datalines; 13 4 5 5 5 5 14 3 4 5 5 5 15 2 3 4 5 5 ; run; data two; input score age; datalines; 5 15 5 15 4 13 5 14 1 13 2 15 1 15 3 15 4 14 3 15 ; run; data twoadj (keep=age score finalscore); array adj{13:15,5} _temporary_; if _n_=1 then do i=1 to 3; set lookup; array adjscore{*} adjscore1-adjscore5; do j=1 to dim(adjscore); adj{age,j}=adjscore{j}; end; end; set two; finalscore=adj{age,score}; run; proc print; run; *PROC TRANSPOSE example; *Read in the table and convert it to long format; data agechart; input length weight1-weight5; datalines; 1 1 1 . . . 2 1 2 2 3 . 3 . 2 3 3 4 4 . . 3 4 4 5 . . 3 4 5 ; proc sort data=agechart; by length; proc transpose data=agechart out=tchart (rename=(age1=age)) name=weight prefix=age; by length; run; proc print; run; *Rename variables and modify their types; data fchart; set tchart; weightc=substr(weight,7,1); ww=input(weightc,1.); drop weight weightc; run; proc print; run; *Read in data; proc format; invalue weight low - 900 = 1 901 - 1300 = 2 1301 - 2000 = 3 2001 - 2500 = 4 2501 - high = 5; invalue length low - 400 = 1 401 - 500 = 2 501 - 600 = 3 601 - 675 = 4 676 - high = 5; run; data striper; input id TL WW; length=input(tl,length.); ww=input(ww,weight.); datalines; 1 523 1340 2 535 1297 3 397 1020 4 615 2115 ; run; proc print; run; *Merge data sets; proc sort data=striper; by length ww; proc sort data=fchart; by length ww; run; data striper_age; merge striper(in=in_s) fchart; by length ww; if in_s; run; proc print; run; *Hash object example; data attendance; input date mmddyy8. id; datalines; 10/27/11 1245 10/27/11 4555 10/27/11 1456 10/29/11 4567 10/29/11 1245 10/29/11 4555 10/31/11 1245 10/31/11 787 ; data workout; format name $20. date mmddyy8.; if _N_=1 then do; declare hash members(); members.definekey("id"); members.definedata("name"); members.definedone(); *SAS did not like the unassigned member 5386 in the original attendance data set; call missing(id,name); members.add(key:787,data:'Sam Crump'); members.add(key:1245,data:'Cale Johnson'); members.add(key:1456,data:'Chris Santino'); members.add(key:4555,data:'Isadore Khuryan'); members.add(key:4567,data:'Armand Cayne'); members.add(key:9877,data:'Ron cole'); end; set attendance; members.find(); run; *Reading a hash object from a data set; data memberdata; input name $17. id locker; datalines; Cale R. Johnson 1245 23 Ronald Cole 9877 54 K. Armand Cayne 4567 206 Samuel Crump 787 202 Isadore Khuryan 4555 87 Christian Santino 1456 27 ; data combined; if _N_=1 then do; *Unexecuted IF loads variable names into program data vector so that they are available when defining the hash object; if 0 then set memberdata; declare hash members(dataset: "memberdata"); members.definekey("id"); members.definedata("name","locker"); members.definedone(); end; set attendance; members.find(key:id); run; proc print data=combined label; label name="Name" id="Member ID" locker="Locker #" date="Date"; format date mmddyy8.; run;