You are right it gives one record per key but what I want as output is not one record per newly generated key, what I am looking for is one record per id, questInd QuestOcc combination.
here is the code that I used-
Data Questions;
Set &outfile;
Key = Trim(ID) || SubStr(Quest,1,2) || SubStr(Quest,4,1);
Occ = Substr(Quest,2,1);
run;
Proc Sort Data=Questions;
by Key Descending Occ;
run;
Data AllInfo;
Length QuestInd QuestOcc $10. TextInd TextOcc $1500.;
Retain QuestInd TextInd QuestOcc TextOcc;
Set Questions;
By Key;
if first.key then
do; QuestInd=" "; TextInd=" "; QuestOcc=" "; TextOcc=" ";end;
/* since I do not know if there are more than two matches I do not know what the condition here is, but it should get u started */
If Occ = '3' or Occ = '5'
Then Do;
QuestInd = Quest;
TextInd = Text;
End;
Else Do; /* I am assuming it is either 2 or 4 at this point from your example */
QuestOcc = Quest;
TextOcc = Text;
End;
If Last.Key then Output;
run;
/*
BELOW IS AN ALTERNATE SOLUTION I CAME UP WITH BUT IT IS NOT SO ELEGANT BUT IT WORKS .
*/
/**FILE FOR INDUSTRY AND OCCUPATION CODING**/
data IO_1(keep=id quest nquest text) IO_2(keep=id quest nquest text);
set &outfile.;
by id quest;
quest=upcase(quest);
if quest in("C2_1" "C2_2" "C2_3" "C2_4" "C2_5"
"C2_6" "C2_7" "C2_8" "C2_9" "C2_10"
"C2_11" "C2_12" "C2_13" "C2_14" "C2_15"
"C3_1" "C3_2" "C3_3" "C3_4" "C3_5"
"C3_6" "C3_7" "C3_8" "C3_9" "C3_10"
"C3_11" "C3_12" "C3_13" "C3_14" "C3_15"
"D4_1" "D4_2" "D4_3" "D4_4" "D4_5"
"D4_6" "D4_7" "D4_8" "D4_9" "D4_10"
"D4_11" "D4_12" "D4_13" "D4_14" "D4_15"
"D5_1" "D5_2" "D5_3" "D5_4" "D5_5"
"D5_6" "D5_7" "D5_8" "D5_9" "D5_10"
"D5_11" "D5_12" "D5_13" "D5_14" "D5_15"
);
/*Create New variable nQuest and assign the value to nQuest based on the Ind and Occ combination.
*/
length nquest $10.;
if quest in("C2_1" "C2_2" "C2_3" "C2_4" "C2_5"
"C2_6" "C2_7" "C2_8" "C2_9" "C2_10"
"C2_11" "C2_12" "C2_13" "C2_14" "C2_15"
"D4_1" "D4_2" "D4_3" "D4_4" "D4_5"
"D4_6" "D4_7" "D4_8" "D4_9" "D4_10"
"D4_11" "D4_12" "D4_13" "D4_14" "D4_15"
) then do; nquest=quest; output IO_1;end;
if quest in("C3_1" "C3_2" "C3_3" "C3_4" "C3_5"
"D5_1" "D5_2" "D5_3" "D5_4" "D5_5") then do;
if quest="C3_1" then nquest="C2_1";
if quest="C3_2" then nquest="C2_2";
if quest="C3_3" then nquest="C2_3";
if quest="C3_4" then nquest="C2_4";
if quest="C3_5" then nquest="C2_5";
if quest="C3_6" then nquest= "C2_6" ;
if quest="C3_7" then nquest= "C2_7" ;
if quest="C3_8" then nquest= "C2_8" ;
if quest="C3_9" then nquest= "C2_9" ;
if quest="C3_10" then nquest="C2_10";
if quest="C3_11" then nquest="C2_11";
if quest="C3_12" then nquest="C2_12";
if quest="C3_13" then nquest="C2_13";
if quest="C3_14" then nquest="C2_14";
if quest="C3_15" then nquest="C2_15";
if quest="D5_1" then nquest="D4_1";
if quest="D5_2" then nquest="D4_2";
if quest="D5_3" then nquest="D4_3";
if quest="D5_4" then nquest="D4_4";
if quest="D5_5" then nquest="D4_5";
if quest="D5_6" then nquest= "D4_6" ;
if quest="D5_7" then nquest= "D4_7" ;
if quest="D5_8" then nquest= "D4_8" ;
if quest="D5_9" then nquest= "D4_9" ;
if quest="D5_10" then nquest="D4_10";
if quest="D5_11" then nquest="D4_11";
if quest="D5_12" then nquest="D4_12";
if quest="D5_13" then nquest="D4_13";
if quest="D5_14" then nquest="D4_14";
if quest="D5_15" then nquest="D4_15";
output IO_2;
end;
run;
proc sort data=IO_1(rename=(quest=questOcc text=textOcc));by id nquest;
proc sort data=IO_2(rename=(quest=questInd text=textInd));by id nquest;
data dsn.SpcDumpIO_05202004(drop=nquest);
merge IO_1
IO_2
;by id nquest;
run;
thanks for taking time out in helping me.
-