Hi all,
I have a data as follow:
Make | Model | Type | Origin | DriveTrain | MSRP | Variable_list |
Acura | MDX | SUV | Asia | All | $36,945 | Make, Model, origin |
Acura | RSX Type S 2dr | Sedan | Asia | Front | $23,820 | Make, Model, Type, origin |
It has a variable called Variable_list (It has list of variable comma separated) I want to fetch value of each variable and store in one variable as show below dataset for variable " Variable_value"
Expected output:
Make | Model | Type | Origin | DriveTrain | MSRP | Variable_list | Variable_value |
Acura | MDX | SUV | Asia | All | $36,945 | Make, Model, origin | Acura, MDX, Asia |
Acura | RSX Type S 2dr | Sedan | Asia | Front | $23,820 | Make, Model, Type, origin | Acura, RSX Type S 2dr, Sedan, Asia |
Hi @surajmetha55,
You can apply the VVALUEX function to the variable names in Variable_list:
/* Create sample data for demonstration */
data have;
set sashelp.cars(obs=2 keep=Make--MSRP);
input Variable_list $50.;
cards;
Make, Model, origin
Make, Model, Type, origin
;
/* Create new variable containing comma-separated values according to Variable_list */
data want;
set have;
length Variable_value $100;
do _n_=1 to countw(Variable_list,',');
Variable_value=catx(', ',Variable_value,vvaluex(scan(Variable_list,_n_,',')));
end;
run;
Hi @surajmetha55,
You can apply the VVALUEX function to the variable names in Variable_list:
/* Create sample data for demonstration */
data have;
set sashelp.cars(obs=2 keep=Make--MSRP);
input Variable_list $50.;
cards;
Make, Model, origin
Make, Model, Type, origin
;
/* Create new variable containing comma-separated values according to Variable_list */
data want;
set have;
length Variable_value $100;
do _n_=1 to countw(Variable_list,',');
Variable_value=catx(', ',Variable_value,vvaluex(scan(Variable_list,_n_,',')));
end;
run;
@FreelanceReinh , Thank you. Perfect solution
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.