turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- addressing a variable by using another variable

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-29-2010 06:09 PM

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-29-2010 06:18 PM

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.

Scott Barry

SBBWorks, Inc.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-29-2010 06:47 PM

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.

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-29-2010 07:44 PM

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-30-2010 05:12 AM

Thanks a lot! It is working perfectly!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-30-2010 09:24 AM

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]

[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]

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-30-2010 12:34 PM

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

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