Hello Listserv!
I am banging my head on a simple problem. I want to update a variable value if another variable = x. Here's a sample of my code. DO IF ANY (ACEA,5783). COMPUTE UNITID=110714. END IF. I have tried using recode... DO IF ANY (ACEA,5783). RECODE UNITID (SYSMIS=110714). END IF. I have tried many variations of the above sample code, with () without (). The result I get is either an error message or the UNITID variable updates all cases with '110714'. I would appreciate any help. With many thanks, Maria |
Administrator
|
Hello Maria. It is not clear what you want to do, because your two snippets of syntax give different results. For example...
* Generate some sample data. NEW FILE. DATASET CLOSE all. DATA LIST list (,) / ACEA UNITID (2F8.0). BEGIN DATA 5783,. 5783,1 5784,2 5785,3 5786,. END DATA. LIST. * Make two copies of variable UNITID. COMPUTE UNITID1 = UNITID. COMPUTE UNITID2 = UNITID. FORMATS UNITID1 UNITID2(F8.0). * Use first syntax snippet to revise UNITID1. DO IF ANY (ACEA,5783). COMPUTE UNITID1=110714. END IF. * NOTE that if 5783 is the only value of ACEA you care * about this reduces to a simple IF command, as follows: * IF ACEA EQ 5783 UNITID1 = 110714. * Use second syntax snippet to revise UNITID2. DO IF ANY (ACEA,5783). RECODE UNITID2 (SYSMIS=110714). END IF. LIST. OUTPUT: ACEA UNITID UNITID1 UNITID2 5783 . 110714 110714 5783 1 110714 1 5784 2 2 2 5785 3 3 3 5786 . . . Number of cases read: 5 Number of cases listed: 5 * Both syntax snippets work with no errors, but * notice that they do not generate the same results. * Which result do you want?. I wonder if you were simply missing an EXECUTE, or a call to some procedure that causes a pass through the dataset. If so, when you look at the data viewer, you'll see a "Transformations pending" message somewhere in the lower right corner. HTH.
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
In reply to this post by Maria Suchard
Is UNITID string variable?? Carlos2015-02-23 15:54 GMT-03:00 Maria Suchard [via SPSSX Discussion] <[hidden email]>: Hello Listserv! |
Hello Carlos,
ACEA is a string variable and UNITID is numeric. Thanks, Maria On 2/23/2015 11:28 AM, lvccrespo [via SPSSX Discussion] wrote:
|
Administrator
|
In reply to this post by Maria Suchard
Perhaps you would help others help you by posting the exact error message as well as detailing the variable types involved in your issue?
I'm guessing ACEA or UnitID or both are string variables and you aren't enclosing the value(s) in quotes? IF ACEA EQ '5783' UnitID=110714 . or IF ACEA EQ '5783' UnitID='110714' would suffice. No need for the extra baggage of DO IF or ANY in this simple case. Great opportunity to RTFM about string vs numeric variables? ----
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?" |
In reply to this post by Maria Suchard
Your first attempt DO IF / COMPUTE looks good (provided the variable ACEA does in fact exist in the data...and that there are cases in the data which contain the value 5783 for that variable ACEA...)
You second attempt DO IF / RECODE technically is good also but it will only recode subset of cases where ACEA=5783 AND UNITID is SYSMIS. The two attempts you post can both be re-written as per below, which perhaps is a little more easier to read: IF (ACEA=5783) UNITID=110714. IF (ACEA=5783 AND NVALID(UNITID)=0) UNITID=110714. Does the error/warning message you receive not shed any light? Try diagnosing it step by step: TEMP. SELECT IF ACEA=5783. FREQ ACEA UNITID. This will temporarily select cases where UNITID=110714 and produce a frequency table of UNITID, where then you can observe 1) How many cases from total remain such that ACEA=5783 and 2) of these cases what are the existing values to which you intend on replacing with some other desired value, in this case 110714. Try break up your work by simply producing frequencies and crosstabs when COMPUTE/IF/RECODES statements do not work as you intended them to do...more often than not (almost always!) it's an error on our part... |
In reply to this post by David Marso
Thank you all for your quick responses. I appreciate the help. I created a new numeric variable for ACEA and the recode statement worked. The issue was differing variable types.
Thanks again. Maria |
Administrator
|
It had nothing to do with differing variable types!
No need to create a new numeric variable for your string ACEA. You simply need to "quote" the value when dealing with string variables! IF (strvar EQ 'value') newvar=whatever. Typically (almost always) SPSS provides fairly diagnostic warnings and error messages. Heed them. Post them with any question if you encounter problems.
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 |