make variable based on variable 1 and 2 but variable 2 supercedes variable 1

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

make variable based on variable 1 and 2 but variable 2 supercedes variable 1

Christopher Chong
Hi, 

If you have the below sample data set:

data list list / id * code1 (A4) code2 (A4).
begin data
1 "I42"  "."
2 "J53" "I21"
3 "B49" "I22"
4 "G49" "."
end data.

How would you create a third variable that is a copy of "code1" EXCEPT that when there is a value in "code2" this third variable should take whatever value is in "code2" instead?

Thanks to anyone who can help.

caky
Reply | Threaded
Open this post in threaded view
|

Re: make variable based on variable 1 and 2 but variable 2 supercedes variable 1

Maurice Vergeer
I guess this should work (untested code)

* if the dot represents a real dot.
string code3 (a3).
compute code3=code1.
if code2 ne "." code3=code2.

or

* if the dot represents a system missing.
string code3 (a3).
compute code3=code1.
if not missing(code2) code3=code2.


Maurice


On Mon, Jun 6, 2011 at 12:20, Christopher Chong <[hidden email]> wrote:
Hi, 

If you have the below sample data set:

data list list / id * code1 (A4) code2 (A4).
begin data
1 "I42"  "."
2 "J53" "I21"
3 "B49" "I22"
4 "G49" "."
end data.

How would you create a third variable that is a copy of "code1" EXCEPT that when there is a value in "code2" this third variable should take whatever value is in "code2" instead?

Thanks to anyone who can help.

caky



--
___________________________________________________________________
Maurice Vergeer
Department of communication, Radboud University  (www.ru.nl)
PO Box 9104, NL-6500 HE Nijmegen, The Netherlands

Visiting Professor Yeungnam University, Gyeongsan, South Korea

Recent publications:
-Vergeer, M. Lim, Y.S. Park, H.W. (forthcoming). Mediated Relations: New Methods to study Online Social Capital. Asian Journal of Communication.
-Vergeer, M., Hermans, L., & Sams, S. (forthcoming). Online social networks and micro-blogging in political campaigning: The exploration of a new campaign tool and a new campaign style. Party Politics.
-Pleijter, A., Hermans, L. & Vergeer, M. (forthcoming). Journalists and journalism in the Netherlands. In D. Weaver & L. Willnat, The Global Journalist in the 21st Century. London: Routledge.
-Eisinga, R., Franses, Ph.H., & Vergeer, M. (2010). Weather conditions and daily television use in the Netherlands, 1996–2005. International Journal of Meteorology.

Webspace
www.mauricevergeer.nl
http://blog.mauricevergeer.nl/
www.journalisteninhetdigitaletijdperk.nl
maurice.vergeer (skype)
___________________________________________________________________





Reply | Threaded
Open this post in threaded view
|

Re: make variable based on variable 1 and 2 but variable 2 supercedes variable 1

Jarrod Teo-2
In reply to this post by Christopher Chong
Hi Christoper,
 
I am a bit confused with your sample data file. It seems that your code2 is string but those missing values are '.' . This is confusing because missing values in string variables are blank while numerical missing values are '.' . Unless if your code2 is actually numerical variables with value labels as in 1=I21 and 2=I22.
 
However, in the syntax that I am providing you, I am assuming that all the missing string values in code2 are really keyed in as '.' and not blanks. So here is the syntax and I hope will help you.
 
string code3 (a8).
do if (code2 eq '.').
compute code3=code1.
else if (code2 ne '.').
compute code3=code2.
end if.
exe.
 
Please note that if your code2 is indeed numerical, then the code above will not work.
 
Warmest Regards
Dorraj Oet 
 

Date: Sun, 5 Jun 2011 23:20:08 -0400
From: [hidden email]
Subject: make variable based on variable 1 and 2 but variable 2 supercedes variable 1
To: [hidden email]

Hi, 

If you have the below sample data set:

data list list / id * code1 (A4) code2 (A4).
begin data
1 "I42"  "."
2 "J53" "I21"
3 "B49" "I22"
4 "G49" "."
end data.

How would you create a third variable that is a copy of "code1" EXCEPT that when there is a value in "code2" this third variable should take whatever value is in "code2" instead?

Thanks to anyone who can help.

caky