Hi SPSSers
I have a dataset with over 1000 cases each containing a four digit numeric code ranging from 111 to 9711. I need to recode these into a series of dummy variables based on the first 2 digits. I would like to combine the name of the code with the digit numbers. For example 111 would become Code01 and 9711 would become Code97. I started doing this as a series of IF statements, but it's long & tedious. I know there has to be an easier way. Unfortunately I don't know how to move the digits into the variable name. Can anyone help me? Thanks in advance. Best, Lisa Lisa T. Stickney Ph.D. Candidate The Fox School of Business and Management Temple University [hidden email] |
Hi, Lisa,
This syntax is far from general, but goes in your case, is it? If your codes are sparse, it produces too many 'empty' columns (with only zeros). Let us know, if it is a problem. *This is just some sample data. DATA LIST LIST /codes (F4). BEGIN DATA 111 112 113 235 1032 1033 1232 9710 9711 111 112 111 END DATA. *This is the solution. COMPUTE two_digt=TRUNC(codes/100). VECTOR Code (97N1). DO REPEAT code=Code1 TO Code97 / i=1 TO 97. COMPUTE code=(two_digt=i). END REPEAT. EXE. Best, Anton > -----Original Message----- > From: SPSSX(r) Discussion [mailto:[hidden email]] > On Behalf Of Lisa Stickney > Sent: Tuesday, April 24, 2007 7:45 AM > To: [hidden email] > Subject: Recode with part of data in variable name? > > Hi SPSSers > > I have a dataset with over 1000 cases each containing a > four digit numeric code ranging from 111 to 9711. I need to > recode these into a series of dummy variables based on the > first 2 digits. I would like to combine the name of the code > with the digit numbers. For example 111 would become Code01 > and 9711 would become Code97. I started doing this as a > series of IF statements, but it's long & tedious. I know > there has to be an easier way. Unfortunately I don't know > how to move the digits into the variable name. Can anyone > help me? Thanks in advance. > > Best, > Lisa > > Lisa T. Stickney > Ph.D. Candidate > The Fox School of Business > and Management > Temple University > [hidden email] > |
Thanks Anton!
Trust me sparsity is not a problem -- I have few missing codes between the numbers. I'll give this a run later today and holler back is there's a problem. Thanks again. Best, Lisa ----- Original Message ----- From: "Anton Balabanov" <[hidden email]> To: "'Lisa Stickney'" <[hidden email]>; <[hidden email]> Sent: Tuesday, April 24, 2007 3:04 AM Subject: RE: Recode with part of data in variable name? > Hi, Lisa, > > This syntax is far from general, but goes in your case, is it? > If your codes are sparse, it produces too many 'empty' columns (with only > zeros). Let us know, if it is a problem. > > *This is just some sample data. > DATA LIST LIST /codes (F4). > BEGIN DATA > 111 > 112 > 113 > 235 > 1032 > 1033 > 1232 > 9710 > 9711 > 111 > 112 > 111 > END DATA. > > *This is the solution. > COMPUTE two_digt=TRUNC(codes/100). > VECTOR Code (97N1). > DO REPEAT code=Code1 TO Code97 / i=1 TO 97. > COMPUTE code=(two_digt=i). > END REPEAT. > EXE. > > > Best, > Anton > >> -----Original Message----- >> From: SPSSX(r) Discussion [mailto:[hidden email]] >> On Behalf Of Lisa Stickney >> Sent: Tuesday, April 24, 2007 7:45 AM >> To: [hidden email] >> Subject: Recode with part of data in variable name? >> >> Hi SPSSers >> >> I have a dataset with over 1000 cases each containing a >> four digit numeric code ranging from 111 to 9711. I need to >> recode these into a series of dummy variables based on the >> first 2 digits. I would like to combine the name of the code >> with the digit numbers. For example 111 would become Code01 >> and 9711 would become Code97. I started doing this as a >> series of IF statements, but it's long & tedious. I know >> there has to be an easier way. Unfortunately I don't know >> how to move the digits into the variable name. Can anyone >> help me? Thanks in advance. >> >> Best, >> Lisa >> >> Lisa T. Stickney >> Ph.D. Candidate >> The Fox School of Business >> and Management >> Temple University >> [hidden email] >> > > |
In reply to this post by Anton Balabanov
Thanks Anton,
This worked like a charm. Now that I look at the results, I have a handful of codes that have empty columns (all zeros or all zeros & a few missing values), so the question becomes: without manually going through 97 codes, is there a way to delete the columns that are empty? Thanks in advance. Best, Lisa ----- Original Message ----- From: "Anton Balabanov" <[hidden email]> To: "'Lisa Stickney'" <[hidden email]>; <[hidden email]> Sent: Tuesday, April 24, 2007 3:04 AM Subject: RE: Recode with part of data in variable name? > Hi, Lisa, > > This syntax is far from general, but goes in your case, is it? > If your codes are sparse, it produces too many 'empty' columns (with only > zeros). Let us know, if it is a problem. > > *This is just some sample data. > DATA LIST LIST /codes (F4). > BEGIN DATA > 111 > 112 > 113 > 235 > 1032 > 1033 > 1232 > 9710 > 9711 > 111 > 112 > 111 > END DATA. > > *This is the solution. > COMPUTE two_digt=TRUNC(codes/100). > VECTOR Code (97N1). > DO REPEAT code=Code1 TO Code97 / i=1 TO 97. > COMPUTE code=(two_digt=i). > END REPEAT. > EXE. > > > Best, > Anton > >> -----Original Message----- >> From: SPSSX(r) Discussion [mailto:[hidden email]] >> On Behalf Of Lisa Stickney >> Sent: Tuesday, April 24, 2007 7:45 AM >> To: [hidden email] >> Subject: Recode with part of data in variable name? >> >> Hi SPSSers >> >> I have a dataset with over 1000 cases each containing a >> four digit numeric code ranging from 111 to 9711. I need to >> recode these into a series of dummy variables based on the >> first 2 digits. I would like to combine the name of the code >> with the digit numbers. For example 111 would become Code01 >> and 9711 would become Code97. I started doing this as a >> series of IF statements, but it's long & tedious. I know >> there has to be an easier way. Unfortunately I don't know >> how to move the digits into the variable name. Can anyone >> help me? Thanks in advance. >> >> Best, >> Lisa >> >> Lisa T. Stickney >> Ph.D. Candidate >> The Fox School of Business >> and Management >> Temple University >> [hidden email] >> > > |
Well, than try a bit more complex code:
*Sample data. DATA LIST LIST /codes (F4). BEGIN DATA 111 112 113 235 1032 1033 1232 9710 9711 111 112 . 11111 END DATA. *This is the solution. COMPUTE two_digt=TRUNC(codes/100). SORT CASES BY two_digt. VECTOR Code (97N1). STRING tempname (A8). DO IF $casenum=1. - WRITE OUTFILE="c:/temp/keepvars.sps" /"ADD FILES FILE=* /KEEP=codes ". END IF. DO REPEAT code=Code1 TO Code97 / i=1 TO 97. COMPUTE code=(two_digt=i). DO IF two_digt=i AND (two_digt~=LAG(two_digt) OR $casenum=1 OR MISS(LAG(two_digt))). - COMPUTE tempname=CONCAT(" Code",LTRIM(STRING(i,F4.0))). - WRITE OUTFILE="c:/temp/keepvars.sps" / tempname . END IF. END REPEAT. EXE. INCLUDE "c:/temp/keepvars.sps". exe. Best, Anton > -----Original Message----- > From: SPSSX(r) Discussion [mailto:[hidden email]] > On Behalf Of Lisa Stickney > Sent: Wednesday, April 25, 2007 4:53 PM > To: [hidden email] > Subject: Re: Recode with part of data in variable name? > > Thanks Anton, > > This worked like a charm. Now that I look at the results, > I have a handful of codes that have empty columns (all zeros > or all zeros & a few missing values), so the question > becomes: without manually going through 97 codes, is there a > way to delete the columns that are empty? Thanks in advance. > > Best, > Lisa > > > ----- Original Message ----- > From: "Anton Balabanov" <[hidden email]> > To: "'Lisa Stickney'" <[hidden email]>; <[hidden email]> > Sent: Tuesday, April 24, 2007 3:04 AM > Subject: RE: Recode with part of data in variable name? > > > > Hi, Lisa, > > > > This syntax is far from general, but goes in your case, is it? > > If your codes are sparse, it produces too many 'empty' > columns (with > > only zeros). Let us know, if it is a problem. > > > > *This is just some sample data. > > DATA LIST LIST /codes (F4). > > BEGIN DATA > > 111 > > 112 > > 113 > > 235 > > 1032 > > 1033 > > 1232 > > 9710 > > 9711 > > 111 > > 112 > > 111 > > END DATA. > > > > *This is the solution. > > COMPUTE two_digt=TRUNC(codes/100). > > VECTOR Code (97N1). > > DO REPEAT code=Code1 TO Code97 / i=1 TO 97. > > COMPUTE code=(two_digt=i). > > END REPEAT. > > EXE. > > > > > > Best, > > Anton > > > >> -----Original Message----- > >> From: SPSSX(r) Discussion > [mailto:[hidden email]] On Behalf > >> Of Lisa Stickney > >> Sent: Tuesday, April 24, 2007 7:45 AM > >> To: [hidden email] > >> Subject: Recode with part of data in variable name? > >> > >> Hi SPSSers > >> > >> I have a dataset with over 1000 cases each containing a four > >> digit numeric code ranging from 111 to 9711. I need to > recode these > >> into a series of dummy variables based on the first 2 digits. I > >> would like to combine the name of the code with the digit > numbers. > >> For example 111 would become Code01 and 9711 would become > Code97. I > >> started doing this as a series of IF statements, but it's long & > >> tedious. I know there has to be an easier way. Unfortunately I > >> don't know how to move the digits into the variable name. > Can anyone > >> help me? Thanks in advance. > >> > >> Best, > >> Lisa > >> > >> Lisa T. Stickney > >> Ph.D. Candidate > >> The Fox School of Business > >> and Management > >> Temple University > >> [hidden email] > >> > > > > > |
Perfect! The only change I made was to the variable list on the KEEP
subcommand (I included all my other variables). Thanks again. Best, Lisa ----- Original Message ----- From: "Anton Balabanov" <[hidden email]> To: <[hidden email]> Sent: Wednesday, April 25, 2007 10:50 AM Subject: Re: Recode with part of data in variable name? > Well, than try a bit more complex code: > > *Sample data. > DATA LIST LIST /codes (F4). > BEGIN DATA > 111 > 112 > 113 > 235 > 1032 > 1033 > 1232 > 9710 > 9711 > 111 > 112 > . > 11111 > END DATA. > > *This is the solution. > COMPUTE two_digt=TRUNC(codes/100). > SORT CASES BY two_digt. > VECTOR Code (97N1). > > STRING tempname (A8). > DO IF $casenum=1. > - WRITE OUTFILE="c:/temp/keepvars.sps" /"ADD FILES FILE=* /KEEP=codes ". > END IF. > DO REPEAT code=Code1 TO Code97 / i=1 TO 97. > COMPUTE code=(two_digt=i). > DO IF two_digt=i AND (two_digt~=LAG(two_digt) OR $casenum=1 OR > MISS(LAG(two_digt))). > - COMPUTE tempname=CONCAT(" Code",LTRIM(STRING(i,F4.0))). > - WRITE OUTFILE="c:/temp/keepvars.sps" / tempname . > END IF. > END REPEAT. > EXE. > INCLUDE "c:/temp/keepvars.sps". > exe. > > Best, > Anton > >> -----Original Message----- >> From: SPSSX(r) Discussion [mailto:[hidden email]] >> On Behalf Of Lisa Stickney >> Sent: Wednesday, April 25, 2007 4:53 PM >> To: [hidden email] >> Subject: Re: Recode with part of data in variable name? >> >> Thanks Anton, >> >> This worked like a charm. Now that I look at the results, >> I have a handful of codes that have empty columns (all zeros >> or all zeros & a few missing values), so the question >> becomes: without manually going through 97 codes, is there a >> way to delete the columns that are empty? Thanks in advance. >> >> Best, >> Lisa >> >> >> ----- Original Message ----- >> From: "Anton Balabanov" <[hidden email]> >> To: "'Lisa Stickney'" <[hidden email]>; <[hidden email]> >> Sent: Tuesday, April 24, 2007 3:04 AM >> Subject: RE: Recode with part of data in variable name? >> >> >> > Hi, Lisa, >> > >> > This syntax is far from general, but goes in your case, is it? >> > If your codes are sparse, it produces too many 'empty' >> columns (with >> > only zeros). Let us know, if it is a problem. >> > >> > *This is just some sample data. >> > DATA LIST LIST /codes (F4). >> > BEGIN DATA >> > 111 >> > 112 >> > 113 >> > 235 >> > 1032 >> > 1033 >> > 1232 >> > 9710 >> > 9711 >> > 111 >> > 112 >> > 111 >> > END DATA. >> > >> > *This is the solution. >> > COMPUTE two_digt=TRUNC(codes/100). >> > VECTOR Code (97N1). >> > DO REPEAT code=Code1 TO Code97 / i=1 TO 97. >> > COMPUTE code=(two_digt=i). >> > END REPEAT. >> > EXE. >> > >> > >> > Best, >> > Anton >> > >> >> -----Original Message----- >> >> From: SPSSX(r) Discussion >> [mailto:[hidden email]] On Behalf >> >> Of Lisa Stickney >> >> Sent: Tuesday, April 24, 2007 7:45 AM >> >> To: [hidden email] >> >> Subject: Recode with part of data in variable name? >> >> >> >> Hi SPSSers >> >> >> >> I have a dataset with over 1000 cases each containing a four >> >> digit numeric code ranging from 111 to 9711. I need to >> recode these >> >> into a series of dummy variables based on the first 2 digits. I >> >> would like to combine the name of the code with the digit >> numbers. >> >> For example 111 would become Code01 and 9711 would become >> Code97. I >> >> started doing this as a series of IF statements, but it's long & >> >> tedious. I know there has to be an easier way. Unfortunately I >> >> don't know how to move the digits into the variable name. >> Can anyone >> >> help me? Thanks in advance. >> >> >> >> Best, >> >> Lisa >> >> >> >> Lisa T. Stickney >> >> Ph.D. Candidate >> >> The Fox School of Business >> >> and Management >> >> Temple University >> >> [hidden email] >> >> >> > >> > >> > |
Free forum by Nabble | Edit this page |