8.10.14

Convert variable name list to comma separated variable names

This trick is quite useful for SPSS. Let say I have 50 variables that I want to sum up the values by using SUM(var1,var2, ...) in Transform > Compute variable..., converting the list:
var1
var2
...
var50
to
var1,var2,...,var50
then
SUM(var1,var2,...var50)
is quite tedious.

I found this nice command line from http://www.shellhacks.com/ (in Linux, of course). Save your list in a file, say temp_file, then
echo $(awk 'NR > 1{print line", "}{line=$0;}END{print $0" "}' temp_file) > new_file
Your comma separated variable names are ready in new_file. Thanks, penguin.

No comments:

Post a Comment