Help using Base SAS procedures

Error in proc transpose

Accepted Solution Solved
Reply
Contributor
Posts: 27
Accepted Solution

Error in proc transpose

Hi,

 

I have data arranged in the following way:

 

IDFoodintake
1food132
1food212
1food369
2food122
2food278
2food356

 

Using the PROC TRANSPOSE procedure, I want to structure it in this way:

IDfood1food2food3
1321269
2227856

 

I have used the following code to restructure:

 

proc transpose data=have out=want;

id food;

var intake;

by ID;

run;

 

However, I repeatedly get the following error message, for each food:

"The ID value "food1" occurs twice in the same BY group."

 

The output seems OK after some random checks, so I wonder why I get this error message. Does it have to do with the length of the character variables (the actual strings are longer than the length of 'food1' etc).

 

Hope someone can help me out!


Accepted Solutions
Solution
‎05-02-2018 08:47 AM
Super User
Posts: 10,787

Re: Error in proc transpose

Posted in reply to Marjolein
Then your data must have something like :



ID	Food	intake
1	food1	32
1	food1	22
1	food1	12
1	food2	12
1	food3	69


use PROC SORT nodupkey; by id food ;run; to have unique Key value.

View solution in original post


All Replies
Solution
‎05-02-2018 08:47 AM
Super User
Posts: 10,787

Re: Error in proc transpose

Posted in reply to Marjolein
Then your data must have something like :



ID	Food	intake
1	food1	32
1	food1	22
1	food1	12
1	food2	12
1	food3	69


use PROC SORT nodupkey; by id food ;run; to have unique Key value.
Contributor
Posts: 27

Re: Error in proc transpose

It worked, thanks a lot!

PROC Star
Posts: 8,167

Re: Error in proc transpose

Posted in reply to Marjolein

If you have more than one record per ID/food combination, I would first ask why. While @Ksharp showed you a way to eliminate duplicate ID/food combinations, possibly you need to run proc summary first to sum the intake values for each ID/food combination.

 

Art, CEO, AnalystFinder.com

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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