BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
yellowyellowred
Obsidian | Level 7

Sorry for the confusing title. Here is what I mean:  
I have a dataset

before_group_3after_group_3before_group_4after_group_4beforeafter
3412before_group_3after_group_3
5634before_group_4after_group_4
7712before_group_3after_group_3

 

 

I want to add another column:

before_group_3after_group_3before_group_4after_group_4beforeafterbefore_evalafter_eval
3412before_group_3after_group_334
561011before_group_4after_group_41011
7712before_group_3after_group_334

 

 

I think that I could do in a datastep:

if before = "before_group_3" then before_eval = before_group_3;

if before = "before_group_4" then before_eval = before_group_4;

 

so I want a function that does this:
before_eval = FUNCTION(before)  
  
I have a lot more groups, e.g. group_99, so that's why I want a function like this

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
infile cards expandtabs;
input before_group_3	after_group_3	before_group_4	after_group_4	before :$20.	after  :$20.;
cards;
3	4	1	2	before_group_3	after_group_3
5	6	3	4	before_group_4	after_group_4
7	7	1	2	before_group_3	after_group_3
;

data want;
 set have;
 before_eval=vvaluex(before);
 after_eval=vvaluex(after);
run;

View solution in original post

5 REPLIES 5
Ksharp
Super User
data have;
infile cards expandtabs;
input before_group_3	after_group_3	before_group_4	after_group_4	before :$20.	after  :$20.;
cards;
3	4	1	2	before_group_3	after_group_3
5	6	3	4	before_group_4	after_group_4
7	7	1	2	before_group_3	after_group_3
;

data want;
 set have;
 before_eval=vvaluex(before);
 after_eval=vvaluex(after);
run;
yellowyellowred
Obsidian | Level 7

Thanks! Is there a function that returns a numeric instead?

jklaverstijn
Rhodochrosite | Level 12

VVALUE(X) is designed to return a character. You can wrap the call to VVALUEX in a call to INPUT:

data want;
 set have;
 before_eval=input(vvaluex(before), best.);
 after_eval=input(vvaluex(after), best.);
run;

- Jan

Tom
Super User Tom
Super User

@yellowyellowred wrote:

Thanks! Is there a function that returns a numeric instead?


Not as simply.

You could convert the string back to a number.

number = input( vvaluex( variable_with_name ) , 32. );

You could do some tricks so you could index into an array.

array numbers var1 var2 var3 ;
array names $32 [3] _temporary_ ('var1','var2','var3');
number = numbers[ whichc( variable_with_name, of names[*]) ] ;

 Or 

array numbers var1 var2 var3 ;
number = numbers[ findw('var1 var2 var3', variable_with_name,' ','e') ] ;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 1014 views
  • 2 likes
  • 5 in conversation