Hello All,
hoping someone can help with this little problem. I have a dataset which looks something like this
| age_1 | age_2 | gender | Mean |
| 1 | 0 | 0 | 15 |
| 0 | 1 | 0 | 32 |
| 0 | 0 | 1 | 23 |
created from a PROC MEANS command and I would like to convert it to a dataset like this
| Var | Mean |
| age_1 | 15 |
| age_2 | 32 |
| gender | 23 |
I had though maybe an ARRAY approach would work, but can't quite figure out how to pass the variable name from the array into a new variable. Perhaps someone could help?
Many thanks, Chris
data have;
input age_1 age_2 gender Mean;
cards;
1 0 0 15
0 1 0 32
0 0 1 23
;
run;
data want;
set have;
length var $ 40;
array x{*} age_1 age_2 gender;
do i=1 to dim(x);
if x{i} then var=vname(x{i});
end;
drop i;
run;
Xia Keshan
Use array:
data want(keep=var mean);
do i=1 by 1 until (last);
set have end=last;
array temp age_1--gender;
var=vname(temp(i));
output;
end;
run;
Or transpose:
proc transpose data=have out=need(where=(col1=1)) name=var;
by mean notsorted;
run;
data have;
input age_1 age_2 gender Mean;
cards;
1 0 0 15
0 1 0 32
0 0 1 23
;
run;
data want;
set have;
length var $ 40;
array x{*} age_1 age_2 gender;
do i=1 to dim(x);
if x{i} then var=vname(x{i});
end;
drop i;
run;
Xia Keshan
many thanks slchen and Xia I'm very grateful you spent time to consider and answer the question. It was the vname function that is key!
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.