Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable in fieldname 2

Status
Not open for further replies.

marye

Technical User
Apr 22, 2003
8
0
0
US
I'd like to use a variable (actually, the results of another field) as part of a fieldname for a column heading. In other words, I have a field, THISYEAR, the value of which will always be the current year (therefore, THISYEAR currently results in "05"). So, I'd like the have the column heading read 05AMOUNT instead of AMOUNT. Of course, next year it would automatically read 06AMOUNT.

Suggestions on how to do this?

Thanks!
Mary E.
 
You can use the Dialogue Manager to get what you want. There are 'system variables', several of which contain the 'current' date, for example &MDY has the current date as a number, in MMDDYY format. So, you might code:

Code:
-SET &THISMONTH = EDIT(&MDY,'99$$$$');
...
PRINT AMOUNT AS &YHISMONTH|AMOUNT
...

The EDIT strips off the first 2 character of the date, providing the month. Using the result (&THISMONTH) in the TABLE, we use the concatenation symbol (|), so it gets concatenated to the front of the 'AMOUNT'.
 
When I try to apply the concat symbol with no spaces between the 2 fieldnames, it seems to think the | is part of the fieldname (i.e. LASTYEAR|AMOUNT appears literally as that). When I try to separate the | from the 2 fieldnames with a space (as you might in a DEFINE), it thinks the first fieldname ends at the space & comes back with an error message. Do you do anything special in the TABLE to get the field concat to work?
 
No go. Column heading still printing literally instead of as 05AMOUNT. :-\
 
Except for the typo (&THISMONTH not &YHISMONTH), what I showed works. As coded, the '|' terminates the amper variable. Not sure WHERE or HOW you're trying to use it between 2 fields, but that will only work in a DEFINE or COMPUTE.
 
Then maybe I don't fully understand your suggestion. I can make the concat symbol work in a DEFINE when creating a value, but I thought you were suggesting I could use it in the PRINT statement (which is where I'd *like* to use it). From your earlier post...

PRINT AMOUNT AS &THISMONTH|AMOUNT

I'm trying to make the field *name*, not the field *value* include the year. I know how to make the field *value* in a DEFINE include a variable by using the concat symbol, but I don't know how to make the field *name* include a variable in a DEFINE. Can I use this PRINT statement to created a defined field *name*?


 
Have you tried this?

Code:
DEFINE FILE	CAR
HEAD1/A20 = 
-SET &HEAD = &MDY | 'AMOUNT';
END
TABLE FILE CAR
PRINT
COUNTRY AS '&HEAD'
END

It should do what you want, just parse out the year (or what ever you want to put).
 
Oh! This worked great!

Now, I only have one other quirk to ask about (right now :) ) for this particular situation...

What if I wanted to concat a field value and a word instead of a system or typed variable and a word? Say I have a field in my MFD (YEAR) whose value is the year. I tried:

-SET &HEAD = YEAR | 'AMOUNT';

but the resulting field name printed out as YEARAMOUNT instead of 05AMOUNT.

Since I might not be looking for data from the current year *or* I might have to get data from more than one year, I can't use a system variable. I see I can enter the variable with a parameter just fine, but I thought it would be simpler (and safer) to use a field value -- if that would work.
 
Try this...
Code:
DEFINE FILE	CAR
HEAD1/A20 = EDIT(SEATS) | ' SEATS';
LINE/A20 = 'AVERAGE COST';
END
TABLE FILE CAR
SUM
LINE AS ' '
SUM
AVE.DEALER_COST AS ' '
ACROSS HEAD1 AS ' '
END
 
There are actually TWO uses for the concatenation symbol. In a calculation (DEFINE, COMPUTE or -SET), it's used to append two field values. That's why:

HEAD1/A20 = EDIT(SEATS) | ' SEATS';

works. Note that you're dealing with field NAMES, or literals (a literal in a DEFINE or COMPUTE is indicated by single quotes).

In Dialogue Manager, a variable is indicated by a leading '&'. If there's no quotes, it's treated as a literal. That's why:

-SET &HEAD = YEAR | 'AMOUNT';

gave you 'YEARAMOUNT'. No amper variables on the right hand side of the '=', so everything was a constant/literal.

A SECOND, less utilized use of the concatenation symbol, is to act as a terminator in a TABLE request. Without the indicator, you couldn't have anything else ABUT an amper variable, as the 'anything else' would be treated as part of the amper variable name. If you had typed:

PRINT AMOUNT AS &THISMONTHAMOUNT

instead of:

PRINT AMOUNT AS &THISMONTH|AMOUNT

The Dialogue Manager would assume it was looking for a D.M. variable named 'THISMONTHAMOUNT'. However, with the concatenation symbol as a separator, it KNOWS that the variable name is THISMONTH, and to position what IMMEDIATELY follow adjacent to the value. Note that there can be NO spaces to the left or right of the concat symbol for this behavior (otherwise it would be treated as a literal). With the concat symbol, if &THISMONTH was '05', the AS phrase becomes '05AMOUNT'.


 
focwizard, I don't quite get your point. Are you saying that my code sample is incorrect? Or are you just explaining in depth how the concat operator works?

Because the sample I gave is from an actual report, that has been in production and It doesn't seem to fully follow your explanation.
 
Mary,

A little of both. I was trying to give a more detailed explanation of using the concat operator ('|').

The only code you posted was:

-SET &HEAD = YEAR | 'AMOUNT';

As I said, use of 'YEAR' in the Dialogue Manager is treated as a literal, since any VARIABLE in the Dialogue Manager must begin with an '&'. The field YEAR is only available, when reporting off a file (i.e. through the TABLE command), where YEAR is a described FIELD. Dialogue Manager knows NOTHING about database fields or values.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top