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

Hello All,

hoping someone can help with this little problem.  I have a dataset which looks something like this

age_1 age_2gender Mean
10015
01032
00123

created from a PROC MEANS command and I would like to convert it to a dataset like this

VarMean
age_115
age_232
gender23

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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

View solution in original post

3 REPLIES 3
slchen
Lapis Lazuli | Level 10

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;

Ksharp
Super User
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

cb23_york
Obsidian | Level 7

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!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1601 views
  • 5 likes
  • 3 in conversation