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

Hello:

I have a table "have" which can be created by the code attached. I would like to reshape it to the table "want" (see eg in the code). Basicly, the table "have" contains the counts of types of variable1 and variable2. It can be thought as types of sales-person, non_sales-person, both types for var1. For var2, it could be locations of "a" or "b". I have tried proc transpose. But I failed to do so since not sure how to handle the varialbe names.

data have;
input year num_s_a num_s_b num_ns_a num_ns_b num_both_a num_both_b;
cards;
2010 1 2 3 4 5 6
2011 7 8 9 10 11 12
;
run;

data want;
input year v1_type $ v2_type $ count;
cards;
2010 s a 1
2010 s b 2
2010 ns a 3
2010 ns b 4
2010 both a 5
2010 both b 6
2011 s a 7
2011 s b 8
2011 ns a 9
2011 ns b 10
2011 both a 11
2011 both b 12
;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Try

data want;
   set have;
   length 
      v1_type v2_type $ 4 count 8
      _varName $ 32
   ;
   
   array vars num_:;
   
   do _i = 1 to dim(vars);
      _varName = vname(vars[_i]);
      v1_type = scan(_varName, 2, '_');
      v2_type = scan(_varName, 3, '_');
      count = vars[_i];
      output;
   end;
   
   drop num_: _:;
run;

View solution in original post

5 REPLIES 5
sasecn
Quartz | Level 8
 
andreas_lds
Jade | Level 19

Try

data want;
   set have;
   length 
      v1_type v2_type $ 4 count 8
      _varName $ 32
   ;
   
   array vars num_:;
   
   do _i = 1 to dim(vars);
      _varName = vname(vars[_i]);
      v1_type = scan(_varName, 2, '_');
      v2_type = scan(_varName, 3, '_');
      count = vars[_i];
      output;
   end;
   
   drop num_: _:;
run;
sasecn
Quartz | Level 8

Thanks, that works. I thought i can use proc transpose, but the data step works!

singhsahab
Lapis Lazuli | Level 10

@sasecn you can try below code:

 

data have;
input year num_s_a num_s_b num_ns_a num_ns_b num_both_a num_both_b;
cards;
2010 1 2 3 4 5 6
2011 7 8 9 10 11 12
;
run;

proc transpose data=have out=have1(rename=(col1=count)) name=varname;
by year notsorted;
var num:;
run;

data want;
set have1;
v1_type=scan(varname,2,'_');
v2_type=scan(varname,3,'_');
drop varname;
run;
sasecn
Quartz | Level 8

Thank you very much! That's what i was looking for. Wish i can have an option to mark yours as solution as well. The system only allows me to mark one.

Thanks again!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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