| Date: | Fri, 21 Aug 1998 11:01:31 +0200 |
| Reply-To: | martin trollope <martint@HOLLARD.CO.ZA> |
| Sender: | "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU> |
| From: | martin trollope <martint@HOLLARD.CO.ZA> |
| Subject: | Re: Indirect var reference in a DATA step |
|
| Content-Type: | text/plain; charset="us-ascii" |
Hi John,
This idea may help:
data try_it;
infile cards;
input B $ A1-A10;
cards;
A1 0 1 2 4 3 5 6 1 2 7
A5 7 4 6 5 7 5 3 9 2 8
A9 1 4 7 4 6 5 7 5 3 9
A7 5 7 5 3 9 2 8 6 5 3
A4 5 3 1 1 4 7 4 6 5 7
;
run;
Where B contains the field name of the field whose value you want in C.
data result(keep=B C);
length B $8.; * 8 being the maximum varname length;
dsid=open("work.try_it","is"); * Open the dataset using SCL-like base
function;
rc = fetch(dsid); * Read the first record;
do while (rc = 0); * Loop until finished;
B = getvarc(dsid,1); * Get the character value of B (var number 1, vary if
not in pos 1);
var = varnum(dsid,B); * Get the position of the var named in B;
C = getvarn(dsid,var); * Get the value of that var into C;
output; * Write record;
rc = fetch(dsid); * Read next record;
end;
rc = close(dsid); * Close dataset;
stop;
run;
You'll have to do some fancy footwork if your vars are mixed char & num.
Hope this helps. Let me know if I can help further.
Martin
----------
From: John Potelle[SMTP:JPotelle@HER-CHER.ORG]
Reply To: John Potelle
Sent: 20 August 1998 08:05
To: SAS-L@AKH-WIEN.AC.AT
Subject: Indirect var reference in a DATA step
I'm using SAS 6.11 on Digital UNIX and SAS 6.12 on Win95.
I have a dataset set up like this: One var (B) contains the name of
another var (out of many possible vars; each obs could be
different). I want to create a third var (C) with the value of the var
specified by (B). Here psudo-code example:
A="This is the value I want";
B="A";
C=Value(B); /* Or some such mechanism.You get the idea */
C should equal "This is the value I want";
Any ideas how this can be in a single data step? If not, how would
you accomplish this?
|