Dear List, I am trying to find an efficient way to code the following task. I do not need a piece of working code, just some new ideas for a better a conceptual solution. Respondents are asked to select the most important issue for them from three lists (all containing the same 16 items), and then they are asked to write examples of each one of these (not compulsory). I need to create
a list of all examples for each issue, where I ignore whether the issue is the first, second or third important. In some ways it resembles a multiple choice, but in theory it is possible to choose same issue several times and thus give several examples related
to the same issue. Now, I am writing a relatively long code since it involves lots of repetition. My code is working, but I do similar things often and would like to find an elegant solution to this puzzle.
I am sure that there is a more efficient and elegant way to solve this, without compromising the readability of the code.
Ideally, each list would be written to its own excel file or on separate worksheets, but this is easy to add manually. However, I can see that in some solutions, like using Python it is perhaps easier to include it into
the code from the beginning. Sincerely, Eero Olli ---------------------------------------------------------------------------------------- Senior Adviser, PhD at Equality and Anti-Discrimination Ombud, Norway OUTPUT NEW NAME=ImportantList. DATASET CLOSE ALL. SET UNICODE=OFF. SET LOCALE='nb_NO.windows-1252'. data list fixed / respondentID 1-3 Important1 5-6 Important2 9-10 Important3 13-15 Text1 20-39 (A) Text2 40-59 (A) Text3 60-79 (A). begin data 001 1 2 4 Getting a job Getting permits Avoid discrimination 002 4 3 1 Avoid hate jobs 003 4 2 3 Just grades safety 004 4 2 1 Avoid discriminati Getting right perm Get to interview 005 2 1 3 Justice Earn right salary
end data . execute . VARIABLE LABELS Important1 "The most important issue is" Important2 "The second most important issue is" Important3 "The third most important issue is" Text1 "An example of the most important issue" Text2 "An example of the second most important issue" Text3 "An example of the third most important issue". STRING example_text (A60). * MOCK CODE – REPEAT REST FOR EVERY ISSUE/VALUE. * Make dummy variables for all values of Important1. IF ANY(1,Important1,Important2, Important3) Dummy_Important=1. FORMATS Dummy_Important (N1). * Create a text, that includes only all relevant examples. * first delete old examples before starting the new issue. COMPUTE Example_text= "". IF Important1=1 example_text =RTRIM(Text1). IF Important2=1 example_text =CONCAT(RTRIM(text2), example_text). IF Important3=1 example_text =CONCAT(RTRIM(text3), example_text). * if no examples are given filter the case out. IF CHAR.LENGTH(example_text)>0 filter_$ = 1. Filter by dummy_Important. FILTER BY filter_$. EXECUTE. *Make a list. SUMMARIZE /TABLES = Example_text RespondentID /FORMAT=VALIDLIST NOCASENUM TOTAL LIMIT=1000 /TITLE='Examples of ISSUE 1' /MISSING=VARIABLE /CELLS=COUNT . |
This kind of problem is well suited to the Python dictionary structure. Each issue would be a key in the dictionary, and the values would be the examples. The values would be set up as a list, so for each issue (key), you would just append the example if it is not empty. After the data are passed, you could create a new summary dataset, but it might be easier just to write a csv file from the Python code and import that into Excel. On Mon, May 2, 2016 at 8:09 AM, Eero Olli <[hidden email]> wrote:
|
In reply to this post by Eero Olli
Here is a starter example using your sample data. begin program. from collections import defaultdict import spssdata exampledict = defaultdict(list) curs = spssdata.Spssdata() for case in curs: for important in range(1,4): exampledict[case[important]].append(case[important+3].rstrip()) curs.CClose() for (key, value) in exampledict.items(): for v in value: if len(v) > 0: print key, v end program. with this output: 1.0 Getting a job 1.0 jobs 1.0 Get to interview 1.0 Earn right salary 2.0 Getting permits 2.0 Getting right perm 2.0 Justice 3.0 safety 4.0 Avoid discrimination 4.0 Avoid hate 4.0 Just grades 4.0 Avoid discriminati On Mon, May 2, 2016 at 8:09 AM, Eero Olli <[hidden email]> wrote:
|
Free forum by Nabble | Edit this page |