|
I know this has been asked on the list multiple times, but I can’t find the right search terms to locate the appropriate thread.
Given input variables APPLE, PEAR and BANANA, how do I compute the variable FRUITS, which concatenates (with comma separator) using VALUELABELS function to retrieve the label to use in the concatenation function?
DATA LIST FREE / id (a2) APPLE (f1.0) PEAR(f1.0) BANANA(f1.0) FRUITS (a999). BEGIN DATA 01 1 0 0 "Apple" 02 0 1 1 "Pear" 03 0 0 1 "Banana" 04 1 1 0 "Apple, Pear" 05 1 0 1 "Apple, Banana" 06 0 1 1 "Banana, Pear" 07 1 1 1 "Apple, Pear, Banana" 08 0 0 0 "" END DATA. VALUE LABELS /APPLE 1 “Apple” /PEAR 1 “Pear” /BANANA 1 “Banana”. The other variation to this is how to produce the variables FIRST SECOND THIRD, where APPLE, PEAR and BANANA represent their rank score for the 3 attributes?
DATA LIST FREE / id (a2) APPLE (f1.0) PEAR(f1.0) BANANA(f1.0) First (f1.0) Second (f1.0) Third (f1.0). BEGIN DATA 01 1 2 3 1 2 3 02 1 3 2 1 3 2 03 2 1 3 2 1 3 04 2 3 1 3 1 2 05 3 1 2 2 3 1 06 3 2 1 3 2 1 END DATA. value labels First Second Third 1 “Apple” 2 “Pear”
3 “Banana”. Many thanks in advance, Jignesh |
|
The first example you could simply do
COMPUTE x = CONCAT(VALUELABEL(X1),", ",VALUELABEL(X2).... To however your liking. Below I've made the example a bit more complicated with the DO REPEAT, but it makes the output a bit nicer and easier to generalize to new examples. **************************************. DATA LIST FREE / id (a2) APPLE (f1.0) PEAR(f1.0) BANANA(f1.0) FRUITS (a999). BEGIN DATA 01 1 0 0 "Apple" 02 0 1 1 "Pear" 03 0 0 1 "Banana" 04 1 1 0 "Apple, Pear" 05 1 0 1 "Apple, Banana" 06 0 1 1 "Banana, Pear" 07 1 1 1 "Apple, Pear, Banana" 08 0 0 0 "" END DATA. VALUE LABELS /APPLE 1 'Apple' /PEAR 1 'Pear' /BANANA 1 'Banana'. STRING #s Fruit2 (A100). DO REPEAT o = APPLE PEAR BANANA. COMPUTE #s = VALUELABEL(o). DO IF #s <> "" AND Fruit2 = "". COMPUTE Fruit2 = #s. ELSE IF #s <> "" AND Fruit2 <> "". COMPUTE Fruit2 = CONCAT(RTRIM(Fruit2),", ",#s). END IF. END REPEAT. EXECUTE. **************************************. The second question you can use a bit of VECTOR trickery to get what you want. Easier to show than to explain. **************************************. DATA LIST FREE / id (a2) APPLE (f1.0) PEAR(f1.0) BANANA(f1.0) First (f1.0) Second (f1.0) Third (f1.0). BEGIN DATA 01 1 2 3 1 2 3 02 1 3 2 1 3 2 03 2 1 3 2 1 3 04 2 3 1 3 1 2 05 3 1 2 2 3 1 06 3 2 1 3 2 1 END DATA. value labels First Second Third 1 “Apple” 2 “Pear” 3 “Banana”. VECTOR V(3,F1.0). DO REPEAT F = APPLE TO BANANA /#i = 1 TO 3. COMPUTE V(F) = #i. END REPEAT. EXECUTE. **************************************. |
|
Administrator
|
In reply to this post by Jignesh Sutar
Jignesh,
see VECTOR and LOOP (Alternatively you could use DO REPEAT) along with CONCAT and VALUELABLE functions. . --
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
| Free forum by Nabble | Edit this page |
