Sorry for the confusing title. Here is what I mean:
I have a dataset
before_group_3 | after_group_3 | before_group_4 | after_group_4 | before | after |
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 |
I want to add another column:
before_group_3 | after_group_3 | before_group_4 | after_group_4 | before | after | before_eval | after_eval |
3 | 4 | 1 | 2 | before_group_3 | after_group_3 | 3 | 4 |
5 | 6 | 10 | 11 | before_group_4 | after_group_4 | 10 | 11 |
7 | 7 | 1 | 2 | before_group_3 | after_group_3 | 3 | 4 |
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
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;
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;
Thanks! Is there a function that returns a numeric instead?
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
@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') ] ;
before_eval = input(scan(before,-1,"_"),best.);
to extract the numeric value from the string.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.