DATA Step, Macro, Functions and more

Columns Name from its Value

Reply
Occasional Contributor
Posts: 15

Columns Name from its Value

Hi ,

I have a table as below :

a1 a2 a3 a4 a5 a6 a7 a8 ------------------ a20
1 2 3 4 5 6 7 8 ------------------ 100

Say , In my caluculation I got results as 8 then I should display a8(columns name) in new columns as below.

a1 a2 a3 a4 a5 a6 a7 a8 ------------------ a20 New_Columns
1 2 3 4 5 6 7 8 ------------------ 100 a8


Please help me in finding columns name from the it's value.

Many Thanks..
Respected Advisor
Posts: 3,799

Re: Columns Name from its Value

VNAME or
VNAMEX

e.g. name = vname(a[8]);

See documentation for details.
SAS Super FREQ
Posts: 8,862

Re: Columns Name from its Value

Hi:
In a DATA step program, you can create new variables with an assignment statement:
[pre]
data newfile;
length New_Columns $2;
....either SET or INFILE/INPUT statements to read data....
...logic to figure out value of new variable ...
New_Columns = 'A8';
run;
[/pre]

What I don't understand is your statement
In my caluculation I got results as 8 then I should display a8(columns name) in new columns as below.

What calculations? The above approach will create a 2 character text variable whose value is always the string 'A8'. However, this might be the wrong approach depending on what you mean by "calculation". Also, you don't show any code or log file for what you've already tried....that might help folks visualize your logic or attempt. Is it possible to return 2 or more values from your calculations??? If so, what would happen?? Could different observations have different values calculated????

cynthia
Super Contributor
Super Contributor
Posts: 3,174

Re: Columns Name from its Value

Posted in reply to Cynthia_sas
Also, an ARRAY may work for your needs, along with VNAME/VNAMEX, if you want concise code paragraph and the ability to detect what variable (name) has a particular value-condition. The ARRAY can declare a list of SAS variables or possibly _NUMERIC_ ...or... _CHARACTER_ to mean all SAS variables of a given type.

Yes, and it's time to crack open the DOC, with focus on the SAS Language Elements guide and the functions. Check the SAS support http://support.sas.com/ website and use its SEARCH facility in addition to the SAS forum archives and SAS system documentation.

Scott Barry
SBBWorks, Inc.
Ask a Question
Discussion stats
  • 3 replies
  • 167 views
  • 0 likes
  • 4 in conversation