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

Hi All,

 

I need help here.


I have this data. I would like to add another 2 columns which is month and tag for 'new' and 'exist' value based on the column name.

 

type | group  | new_201803 | exist_201803 | new_201804 | exist_201804|
A    |Employer|          15|            21|          10|           36|
A    |Employee|          20|            17|          22|           37|
B |Employer| 2| 8| 10| 10|
B |Employee| 13| 20| 7| 33|

Expected data is like below:

 

type | group  |month  |tag        | amount|
A    |Employer|201803 |        new|     15|
A |Employer|201803 | exist| 21|
A |Employer|201804 | new| 10|
A |Employer|201804 | exist| 36| A |Employee|201803 | new| 20|
A |Employee|201803 | exist| 17| A |Employee|201804 | new| 22|
A |Employee|201804 | exist| 37| B |Employer|201803 | new| 2|
B |Employer|201803 | exist| 8|
B |Employer|201804 | new| 10|
B |Employer|201804 | exist| 10|
B |Employee|201803 | new| 13|
B |Employee|201803 | exist| 20|
B |Employee|201804 | new| 7|
B |Employee|201804 | exist| 33|

Currently I just arranged them in manual way. I would like to use Code but don't know where to start.

Using script can allow me arranging bigger table.

 

Thanks in advance!

 

Regards

    

 



1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

One way

 

data have;
input type $ group $ new_201803 exist_201803 new_201804 exist_201804;
datalines;
A Employer 15 21 10 36
A Employee 20 17 22 37
B Employer 2 8 10 10
B Employee 13 20 7 33
;

proc sort data=have;
	by type group;
run;

proc transpose data=have out=temp;
   by type group;
run;

data want;
	format type group month tag amount;
	set temp;
	rename COL1=Amount;
	month=input(compress(_NAME_, '', 'kd'), best.);
	tag=compress(_NAME_, '_', 'd');
	drop _NAME_;
run;

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

One way

 

data have;
input type $ group $ new_201803 exist_201803 new_201804 exist_201804;
datalines;
A Employer 15 21 10 36
A Employee 20 17 22 37
B Employer 2 8 10 10
B Employee 13 20 7 33
;

proc sort data=have;
	by type group;
run;

proc transpose data=have out=temp;
   by type group;
run;

data want;
	format type group month tag amount;
	set temp;
	rename COL1=Amount;
	month=input(compress(_NAME_, '', 'kd'), best.);
	tag=compress(_NAME_, '_', 'd');
	drop _NAME_;
run;
PeterClemmensen
Tourmaline | Level 20

Alternatively

 

data have;
input type $ group $ new_201803 exist_201803 new_201804 exist_201804;
datalines;
A Employer 15 21 10 36
A Employee 20 17 22 37
B Employer 2 8 10 10
B Employee 13 20 7 33
;

data want(keep=type group month tag amount);
	set have;
	array _{*} _numeric_;
	do i=1 to dim(_);
		tag=compress(vname(_[i]), '_', 'd');
		month=input(compress(vname(_[i]), '', 'kd'), best.);
		amount=_[i];
		output;
	end;
run;
Tom
Super User Tom
Super User

If you had a combination of variables that uniquely identified the row then you could use PROC TRANSPOSE.

Use those variables in your BY statement for PROC TRANSPOSE.

proc transpose data=have out=step2 ;
  by type group ;
  var new_: exist_: ;
run;

Then you can parse the _NAME_ field to get the two pieces of information out of that metadata and into actual data.

data want;
  set step2;
  length month 8 tag $8  ;
  tag=scan(_name_,1,'_');
  month = input(scan(_name_,2,'_'),yymmn6.);
  format month yymm7.;
  rename col1=amount;
run;

If you don't have unique BY groups then you could just add a step to create a new variable to uniquely id each row.

data step1;
  row + 1;
  set have;
run;

Then use that in your BY statement for PROC TRANSPOSE.

proc transpose data=step1 out=step2 ;
  by row type group ;
  var new_: exist_: ;
run;

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 565 views
  • 1 like
  • 3 in conversation