BookmarkSubscribeRSS Feed
mattb
Calcite | Level 5
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
6 REPLIES 6
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
mattb
Calcite | Level 5
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
mattb
Calcite | Level 5
Thanks a lot! It is working perfectly!
data_null__
Jade | Level 19
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]
  • deleted_user
    Not applicable
    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

    sas-innovate-2024.png

    Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

    Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

     

    Register now!

    How to Concatenate Values

    Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

    Find more tutorials on the SAS Users YouTube channel.

    Click image to register for webinarClick image to register for webinar

    Classroom Training Available!

    Select SAS Training centers are offering in-person courses. View upcoming courses for:

    View all other training opportunities.

    Discussion stats
    • 6 replies
    • 837 views
    • 0 likes
    • 4 in conversation