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
- /
- Refer variable value as variable name and assign t...

Topic Options

- 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
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

09-08-2012 01:44 AM

Hello

My objective is to refer a variable value to variable name and assigns a value corrosponds to that variable name. Suppose , I have a dataset.

Obs x1 x2 x3 b c

1 1 2 3 x1 x2

2 2 3 5 x2 x3

3 5 3 6 x1 x3

I would like to create a new variable called D. In this variable, If, I call variable name B, it will give value as x1. What I am looking for is value 1 since x1 variable value for that observation is 1.

Similarly , for second observation, it should call 3 since b value is x2 which refer to 3.

If this is possible, I would furthur like to use this in the Sum(of..) function to sum the values for example: for 1st observation: x1--x2 gives 3, second observation: 8, third observation: 14.

Thanks a lot for all the help.

Accepted Solutions

Solution

09-08-2012
03:02 AM

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

Posted in reply to bnarang

09-08-2012 03:02 AM

d is missing, set it to 0 at the beginning.

missing value + something is missing in SAS.

data new1;

set new;

flag=0;

d=0;

array x(3) x1-x3;

do i=1 to 3;

if vname(x(i))=b then flag=1;

else if vname(x(i))=c then flag=0;

if flag=1 then d=d+x(i);

end;

run;

All Replies

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

Posted in reply to bnarang

09-08-2012 02:08 AM

Can you post what the output would look like?

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

Posted in reply to Reeza

09-08-2012 02:17 AM

Hi Reeza

If I am summing the variables, the output should look like:

Obs | x1 | x2 | x3 | b | c | d |

1 | 1 | 2 | 3 | x1 | x2 | 3 |

2 | 2 | 3 | 5 | x2 | x3 | 8 |

3 | 5 | 3 | 6 | x1 | x3 | 14 |

so here, we have a variable d. D sums the x1--x2 in first observation. x2--x3 in second observation and so on.

Thnaks

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

Posted in reply to bnarang

09-08-2012 02:22 AM

you can probably do it using vname function and an array.

loop through and start adding when you the name matches b and end when it matches c.

flag=0;

array x(3) x1-x3;

do i=1 to 3;

if vname(x(i))=b then flag=1;

else if vname(x(i))=c then flag=0;

if flag=1 then d=d+x(i);

end;

run;

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

Posted in reply to Reeza

09-08-2012 02:37 AM

Sorry. But this did not work. In SAS, the output is missing values. What I wrote is:

data new;

input x1 x2 x3 b $ c $ ;

datalines;

1 2 3 x1 x2

2 3 5 x2 x3

5 3 6 x1 x3

;

proc print;run;

data new1;

set new;

flag=0;

array x(3) x1-x3;

do i=1 to 3;

if vname(x(i))=b then flag=1;

else if vname(x(i))=c then flag=0;

if flag=1 then d=d+x(i);

end;

run;

proc print;run;

The output is:

Obs | x1 | x2 | x3 | b | c | flag | i | d |

1 | 1 | 2 | 3 | x1 | x2 | 0 | 4 | . |

2 | 2 | 3 | 5 | x2 | x3 | 0 | 4 | . |

3 | 5 | 3 | 6 | x1 | x3 | 0 | 4 | . |

In the log, it gives the note as follows:

NOTE: Missing values were generated as a result of performing an operation on missing values.

Each place is given by: (Number of times) at (Line)Column).

4 at 167:19

NOTE: There were 3 observations read from the data set WORK.NEW.

NOTE: The data set WORK.NEW1 has 3 observations and 8 variables.

NOTE: DATA statement used (Total process time):

real time 0.01 seconds

cpu time 0.01 seconds

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

Posted in reply to bnarang

09-08-2012 02:53 AM

Yup the note tells you whats wrong.

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

Posted in reply to Reeza

09-08-2012 03:01 AM

Sorry Reeza

I am not able to solve this problem. Can you please highlight what is wrong with the code now? i am clueless

Thanks

Solution

09-08-2012
03:02 AM

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

Posted in reply to bnarang

09-08-2012 03:02 AM

d is missing, set it to 0 at the beginning.

missing value + something is missing in SAS.

data new1;

set new;

flag=0;

d=0;

array x(3) x1-x3;

do i=1 to 3;

if vname(x(i))=b then flag=1;

else if vname(x(i))=c then flag=0;

if flag=1 then d=d+x(i);

end;

run;

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

Posted in reply to Reeza

09-08-2012 03:11 AM

Thanks a lot Reeza. It worked like charm now. In the second condition, i also marked the flag=1 then only, I could add to the total.

Thank you so much again for helping.

Regards

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

Posted in reply to bnarang

09-08-2012 01:40 PM

hi ... you have a solution, but here's another another idea ...

**data x;**

**input x1-x3 (b c) (:$2.);**

**datalines;**

**1 2 3 x1 x2**

**2 3 5 x2 x3**

**5 3 6 x1 x3**

**;**

**data y;**

**set x;**

**array x(3);**

**do _n_=input(char(b,2),1.) to input(char(c,2),1.);**

** d = sum(d, x(_n_));**

**end;**

**run;**

**x1 x2 x3 b c d**

** 1 2 3 x1 x2 3**

** 2 3 5 x2 x3 8**

** 5 3 6 x1 x3 14**

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

Posted in reply to MikeZdeb

09-08-2012 02:50 PM

Thanks Mike. This also worked like charm and quite neat.