Often I have strings in my SPSS data set which have values like: Y,Yes,yes,No,n, etc. and I want them to be a numeric value so I can take their mean. This SPSS macro makes converting them a breeze! You might need to adapt it to your data set but the principle can be applied for many uses.
The below video demonstrates it usage. Definitely saves a lot of time if o
SPSS macro
These SPSS macros should regularly categorize the string variables correctly.
*/////////////////////. DEFINE !con2Num (vnames=!CMDEND) !DO !vname !IN (!vnames) If INDEX(Upcase(!Vname),Upcase("Y"))>0 Temp1234= 1 . If INDEX(Upcase(!Vname),Upcase("N"))>0 Temp1234= 0 . FORMAT temp1234(F3.0). Val Labels Temp1234 0"No" 1"Yes". Var level Temp1234 (Scale). MATCH FILES FILE=* /DROP=!vname . RENAME VARIABLE (temp1234=!vname). !DOEND exe. !ENDDEFINE. */////////////////////. !con2Num vnames=var1 var2 var3 . */////////////////////. DEFINE !con2NumNA (vnames=!CMDEND) !DO !vname !IN (!vnames) If INDEX(Upcase(!Vname),Upcase("N"))>0 Temp1234= 0 . If INDEX(Upcase(!Vname),Upcase("Y"))>0 Temp1234= 1 . If INDEX(Upcase(!Vname),Upcase("N/A"))>0 OR INDEX(Upcase(!Vname),Upcase("NA"))>0 Temp1234= 2 . FORMAT temp1234(F4.0). Var level Temp1234 (Nominal). Val Labels Temp1234 0"No" 1"Yes" 2"NA". MATCH FILES FILE=* /DROP=!vname . RENAME VARIABLE (temp1234=!vname). !DOEND exe. !ENDDEFINE. */////////////////////. !con2NumNA vnames=var1 var2 var3 .
Here are some convenient references to learn more.
Learning SPSS Macros | Raynald’s SPSS Tools