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

Hi all!

I am having trouble transposing my data. 

Here is what I have:

NameMonthValueCity
JonJan11Orlando
JonFeb42Orlando
JonMar34Orlando
JonApr82Orlando
JillJan24Miami
JillFeb4Miami
JillMar45Miami
CamJan98Houston
CamFeb42Houston
CamMar48Houston
CamApr11Houston
TerryFeb68Austin
TerryApr72Houston

 

Here is what I need:

NameJanFebMarAprCity
Jon11423482Orlando
Jill24445 Miami
Cam98424811Houston
Terry 68 72Error

 

I know proc transpose can be used to transpose however I do not know how to achieve my end table.  Notice I put "Error" as the City for Terry; this is because Terry has more than one city for him and I want that to be flagged. 

Does anyone have any thoughts on this?

Thank you so much for all your help!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data have;
input Name $	Month $	Value	City $;
cards;
Jon	Jan	11	Orlando
Jon	Feb	42	Orlando
Jon	Mar	34	Orlando
Jon	Apr	82	Orlando
Jill	Jan	24	Miami
Jill	Feb	4	Miami
Jill	Mar	45	Miami
Cam	Jan	98	Houston
Cam	Feb	42	Houston
Cam	Mar	48	Houston
Cam	Apr	11	Houston
Terry	Feb	68	Austin
Terry	Apr	72	Houston
;

proc sql;
create table temp as
select Name,Month, Value, ifc(count(distinct city)>1,'Error',City) as  City
from have
group by name ;
quit;

proc transpose data=temp out=want;
by name city notsorted;
var value;
id month;
run;

 

Hi @Singham20  Since you mentioned "I know proc transpose can be used to transpose however I do not know how to achieve my end table." 

I believe you can get the order of the month right by modifying my code in the previous step before tranpose to get the right order. If you are unable to, let us know. In that case, i will have to get over my laziness

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

data have;
input Name $	Month $	Value	City $;
cards;
Jon	Jan	11	Orlando
Jon	Feb	42	Orlando
Jon	Mar	34	Orlando
Jon	Apr	82	Orlando
Jill	Jan	24	Miami
Jill	Feb	4	Miami
Jill	Mar	45	Miami
Cam	Jan	98	Houston
Cam	Feb	42	Houston
Cam	Mar	48	Houston
Cam	Apr	11	Houston
Terry	Feb	68	Austin
Terry	Apr	72	Houston
;

proc sql;
create table temp as
select Name,Month, Value, ifc(count(distinct city)>1,'Error',City) as  City
from have
group by name ;
quit;

proc transpose data=temp out=want;
by name city notsorted;
var value;
id month;
run;

 

Hi @Singham20  Since you mentioned "I know proc transpose can be used to transpose however I do not know how to achieve my end table." 

I believe you can get the order of the month right by modifying my code in the previous step before tranpose to get the right order. If you are unable to, let us know. In that case, i will have to get over my laziness

Singham20
Obsidian | Level 7

Thank you so much!  This does work!  I know how to get the order for the months if it was in numeric form however how would I order the months in there current form?

Thank you!

novinosrin
Tourmaline | Level 20

@Singham20   Ok no worries, try this

 

data have;
input Name $	Month $	Value	City $;
cards;
Jon	Jan	11	Orlando
Jon	Feb	42	Orlando
Jon	Mar	34	Orlando
Jon	Apr	82	Orlando
Jill	Jan	24	Miami
Jill	Feb	4	Miami
Jill	Mar	45	Miami
Cam	Jan	98	Houston
Cam	Feb	42	Houston
Cam	Mar	48	Houston
Cam	Apr	11	Houston
Terry	Feb	68	Austin
Terry	Apr	72	Houston
;
data temp;
do until(last.name);
set have;
by name city notsorted;
if first.city then n=sum(n,1);
end;
do until(last.name);
set have;
by name city notsorted;
City=ifc(n>1,'Error',City);
output;
end;
drop n;
run;


proc transpose data=temp out=want;
by name city notsorted;
var value;
id month;
run;
ballardw
Super User

@Singham20 wrote:

Thank you so much!  This does work!  I know how to get the order for the months if it was in numeric form however how would I order the months in there current form?

Thank you!


Anytime sort order of anything resembling dates or times comes up it is usually worth the effort to get a SAS date value and use an appropriate format.

Partially because just changing the format can give you different groups such as calendar quarter of the year.

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
  • 4 replies
  • 825 views
  • 0 likes
  • 3 in conversation