SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AlexeyS
Pyrite | Level 9

Hi,

My dataset looks like this. I have an array  named da that contains 5 variables, and i want to retrieve a value of variable name from array, based on variable value(varname variable) and return probability(prob).

For example,

For the first row, the probability for variable da1 variable is 0.1.

For the second row, the probability for variable da10 variable is 0.

For the third row, the probability for variable da5 variable is 0.9.

Idda1da2da3da5da10varnameprob
10.10.20.50.150.15da10.1
20.20.60.10.10da100
3000.10.90da50.9

 

Thank you,

Alexey

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Use the VVALUEX() function.

prob = vvaluex(varname);

Small catch - it returns a character value, the formatted displayed value, by default so you'll need to convert it to numeric if required.

 


@AlexeyS wrote:

Hi,

My dataset looks like this. I have an array  named da that contains 5 variables, and i want to retrieve a value of variable name from array, based on variable value(varname variable) and return probability(prob).

For example,

For the first row, the probability for variable da1 variable is 0.1.

For the second row, the probability for variable da10 variable is 0.

For the third row, the probability for variable da5 variable is 0.9.

Id da1 da2 da3 da5 da10 varname prob
1 0.1 0.2 0.5 0.15 0.15 da1 0.1
2 0.2 0.6 0.1 0.1 0 da10 0
3 0 0 0.1 0.9 0 da5 0.9

 

Thank you,

Alexey


 

 

View solution in original post

2 REPLIES 2
Reeza
Super User

Use the VVALUEX() function.

prob = vvaluex(varname);

Small catch - it returns a character value, the formatted displayed value, by default so you'll need to convert it to numeric if required.

 


@AlexeyS wrote:

Hi,

My dataset looks like this. I have an array  named da that contains 5 variables, and i want to retrieve a value of variable name from array, based on variable value(varname variable) and return probability(prob).

For example,

For the first row, the probability for variable da1 variable is 0.1.

For the second row, the probability for variable da10 variable is 0.

For the third row, the probability for variable da5 variable is 0.9.

Id da1 da2 da3 da5 da10 varname prob
1 0.1 0.2 0.5 0.15 0.15 da1 0.1
2 0.2 0.6 0.1 0.1 0 da10 0
3 0 0 0.1 0.9 0 da5 0.9

 

Thank you,

Alexey


 

 

Tom
Super User Tom
Super User

VVALUEX() will work, but it returns the formatted value, so the result is always character.

If you want a numeric result you could use the INPUT() function, but you might lose some precision.

prob=input(vvaluex(varname),32.);

To get the actual value you might need to scan an array.

array da da1-da3 da5 da10 ;
prob=.;
do index=1 to dim(da) while (prob=.);
  if upcase(vname(da[index]))=upcase(varname) then prob=da[index];
end;
drop index;

array.

 

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 2052 views
  • 0 likes
  • 3 in conversation