DATA Step, Macro, Functions and more

addressing a variable by using another variable

Reply
New Contributor
Posts: 3

addressing a variable by using another variable

hello,
i have the following problem: For each observation in my dataset i have hundreds of columns. They are named: day_1 day_2 day_3 day_4 day_5.....
Then I have a variable that contains a number, let's say n=4.

Now I want to create another variable, let's say 'result', that is equal to day_n.

In my example it should be: result = day_4
But n is not always 4. So I need a small programme that helps me to solve that problem.

Thanks in advance for your help.
matt
Super Contributor
Super Contributor
Posts: 3,174

Re: addressing a variable by using another variable

Assign a new SAS CHARACTER variable and use the one of the CAT (concatenate) related functions in a DATA step. Or look at using the VVALUE or VVALUEX function if you are going to assign a value based on value(s) of other variables in the same observation.

Scott Barry
SBBWorks, Inc.
New Contributor
Posts: 3

Re: addressing a variable by using another variable

Thanks for your prompt answer. But I cannot see how these functions would help me with my problem.

I try to explain it in another way:

price_1 = 10;
number = 1;
result = ( value of price_number ) = 10

So all I need is a function that gives me the value of "price_number" where "price_" is a prefix and number is the value of another variable in the same observation.
Super Contributor
Super Contributor
Posts: 3,174

Re: addressing a variable by using another variable

You have a prefix constant 'price_' (or maybe it is stored in a SAS variable) and you have another SAS variable that contains a number (suffix) the one that identifies your desired variable with the data-value.

In a DATA step, use the VVALUEX function and concatenate the variable pieces together to construct a string which in itself is the SAS variable you want to reference.

Have a look at the SAS Language Reference and the VVAULEX function discussion, closely. Here's a link:

http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a002233818.htm

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search argument, this topic/post:
vvaluex function site:sas.com
New Contributor
Posts: 3

Re: addressing a variable by using another variable

Thanks a lot! It is working perfectly!
Respected Advisor
Posts: 3,799

Re: addressing a variable by using another variable

I must have missed something in your original description because I was thinking ARRAY.

[pre]
data a;
array day_[5];
input day_
  • ;
    n = ceil(ranuni(11345)*dim(day_));
    result = day_;
    cards;
    1 2 3 4 5
    5 6 7 8 9
    3 4 5 6 7
    ;;;;
    run;
    proc print;
    run;
    [/pre]
  • N/A
    Posts: 0

    Re: addressing a variable by using another variable

    You have this problem because you are violating an old programming truism: information belongs in variable values, not in variable names.

    The eliminate this, transpose the data set (proc transpose with the name option will do this easily) so your new structure has two fields, like this:

    Day Value
    1 1234
    2 5678
    3 whatever

    Your problem has just gone away. You can find the day(s) you want with a subsetting if or a where statement or data set option. The where can be used in a proc step as well, saving preproceesing.

    Cheers,

    Jonathan

    > hello,
    > i have the following problem: For each observation in
    > my dataset i have hundreds of columns. They are
    > named: day_1 day_2 day_3 day_4 day_5.....
    > Then I have a variable that contains a number, let's
    > say n=4.
    >
    > Now I want to create another variable, let's say
    > 'result', that is equal to day_n.
    >
    > In my example it should be: result = day_4
    > But n is not always 4. So I need a small programme
    > that helps me to solve that problem.
    >
    > Thanks in advance for your help.
    > matt
    Ask a Question
    Discussion stats
    • 6 replies
    • 136 views
    • 0 likes
    • 4 in conversation