Saving text from a data field or table as SYNTAX?

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

Saving text from a data field or table as SYNTAX?

Heidi Green

Hello-

 

I have a 17.0.2 data set that has only 1 field containing a string variable, called Macro1 (A40). See table ‘Macro1’ below.

 

I’ve created this field through syntax based on a sort order of sales by brand in an aggregated data file.

 

 

MACRO1

DEFINE  !s1() BRAND_C !ENDDEFINE.

DEFINE  !s2() BRAND_A !ENDDEFINE.

DEFINE  !s3() BRAND_D !ENDDEFINE.

DEFINE  !s4() BRAND_B !ENDDEFINE.

DEFINE  !s5() BRAND_E !ENDDEFINE.

DEFINE !s99() Others !ENDDEFINE.

 

 

What I would like to do is to automatically create an .sps (syntax) file containing just these statements. (Call it FILE='C:\BlahBlah\Macro1_Insert.sps'.)

 

I would like to then call that syntax from my main syntax file and use the macros to rename some field names in a separate data file.

 

Such as:

 

INSERT FILE='C:\BlahBlah\Macro1_Insert.sps'.

 

RENAME VARIABLES (S1 = !s1)  (S2 = !s2)  (S3 = !s3)  (S4 = !s4)  (S5 = !s5)  (S99= !s99).

 

This is the format of the data set that I am renaming variables in.

 

Geography Name

GrandTotal

S1

S2

S3

S4

S5

S99

Grand Total

14,139

3,585

3,173

2,378

1,945

1,361

15

County 1

752

111

304

66

145

41

0

County 2

517

73

214

77

65

9

0

County 3

473

229

11

49

70

56

1

Etc... Counties

XXX

XXX

XXX

XXX

XXX

XXX

XX

 

 

So after running the rename command, the table should look like:

 

Geography Name

GrandTotal

Brand_C

Brand_A

Brand_D

Brand_B

Brand_E

Others

Grand Total

14,139

3,585

3,173

2,378

1,945

1,361

15

County 1

752

111

304

66

145

41

0

County 2

517

73

214

77

65

9

0

County 3

473

229

11

49

70

56

1

Etc... Counties

XXX

XXX

XXX

XXX

XXX

XXX

XX

 

 

The reason that I’ve ended up in this crazy situation is that:

A). The client wants the format as shown above (in Excel, in fact)

B). The final table must always show the top 20 brands (I’ve only shown 5 in my example), sorted descending Left to Right, with S1 having the highest sales, and all other that don’t make the top 20 are included in S99 which receive the label of “Others”.

C). The total number of brands varies each time the data is processed, and brands move in and out of the top 20 and change their order with each run.

D). I want to be able to run the same syntax every time without having to change it for each run’s specific brands or new order.

 

It’s essentially a Pivot table, but I couldn’t quite get the right configuration using Custom Tables, so I am trying to get the data as close to my final formatting as possible through SPSS, before I drop it into an Excel template.

 

I’m sure there are many different ways to get from my raw data to my end result, (and I’m definitely open to hearing them), but since I’m already at this point, I need to know if I can save a data field or custom table/case summaries table (like ‘Macro1’ above) from the Output viewer as a syntax file that can be called automatically without any manual interaction. I offer all this extra explanation for those of you who might ask, “but why?” J

 

 

Thanks for considering my dilemma, and I appreciate hearing any thoughts/hints you may have.

 

Reply | Threaded
Open this post in threaded view
|

Re: Saving text from a data field or table as SYNTAX?

Heidi Green

I have solved my own problem! Using the WRITE OUTFILE command and changing it to .sps instead of .txt seems to work!

 

WRITE OUTFILE='K:\Cntyboat\Macro1_Insert.sps'

  TABLE /macro1  .

 


From: Heidi Green
Sent: Thursday, June 25, 2009 10:26 AM
To: [hidden email]
Subject: Saving text from a data field or table as SYNTAX?

 

Hello-

 

I have a 17.0.2 data set that has only 1 field containing a string variable, called Macro1 (A40). See table ‘Macro1’ below.

 

I’ve created this field through syntax based on a sort order of sales by brand in an aggregated data file.

 

 

MACRO1

DEFINE  !s1() BRAND_C !ENDDEFINE.

DEFINE  !s2() BRAND_A !ENDDEFINE.

DEFINE  !s3() BRAND_D !ENDDEFINE.

DEFINE  !s4() BRAND_B !ENDDEFINE.

DEFINE  !s5() BRAND_E !ENDDEFINE.

DEFINE !s99() Others !ENDDEFINE.

 

 

What I would like to do is to automatically create an .sps (syntax) file containing just these statements. (Call it FILE='C:\BlahBlah\Macro1_Insert.sps'.)

 

I would like to then call that syntax from my main syntax file and use the macros to rename some field names in a separate data file.

 

Such as:

 

INSERT FILE='C:\BlahBlah\Macro1_Insert.sps'.

 

RENAME VARIABLES (S1 = !s1)  (S2 = !s2)  (S3 = !s3)  (S4 = !s4)  (S5 = !s5)  (S99= !s99).

 

This is the format of the data set that I am renaming variables in.

 

Geography Name

GrandTotal

S1

S2

S3

S4

S5

S99

Grand Total

14,139

3,585

3,173

2,378

1,945

1,361

15

County 1

752

111

304

66

145

41

0

County 2

517

73

214

77

65

9

0

County 3

473

229

11

49

70

56

1

Etc... Counties

XXX

XXX

XXX

XXX

XXX

XXX

XX

 

 

So after running the rename command, the table should look like:

 

Geography Name

GrandTotal

Brand_C

Brand_A

Brand_D

Brand_B

Brand_E

Others

Grand Total

14,139

3,585

3,173

2,378

1,945

1,361

15

County 1

752

111

304

66

145

41

0

County 2

517

73

214

77

65

9

0

County 3

473

229

11

49

70

56

1

Etc... Counties

XXX

XXX

XXX

XXX

XXX

XXX

XX

 

 

The reason that I’ve ended up in this crazy situation is that:

A). The client wants the format as shown above (in Excel, in fact)

B). The final table must always show the top 20 brands (I’ve only shown 5 in my example), sorted descending Left to Right, with S1 having the highest sales, and all other that don’t make the top 20 are included in S99 which receive the label of “Others”.

C). The total number of brands varies each time the data is processed, and brands move in and out of the top 20 and change their order with each run.

D). I want to be able to run the same syntax every time without having to change it for each run’s specific brands or new order.

 

It’s essentially a Pivot table, but I couldn’t quite get the right configuration using Custom Tables, so I am trying to get the data as close to my final formatting as possible through SPSS, before I drop it into an Excel template.

 

I’m sure there are many different ways to get from my raw data to my end result, (and I’m definitely open to hearing them), but since I’m already at this point, I need to know if I can save a data field or custom table/case summaries table (like ‘Macro1’ above) from the Output viewer as a syntax file that can be called automatically without any manual interaction. I offer all this extra explanation for those of you who might ask, “but why?” J

 

 

Thanks for considering my dilemma, and I appreciate hearing any thoughts/hints you may have.