Hi,
I have data arranged in the following way:
ID | Food | intake |
1 | food1 | 32 |
1 | food2 | 12 |
1 | food3 | 69 |
2 | food1 | 22 |
2 | food2 | 78 |
2 | food3 | 56 |
Using the PROC TRANSPOSE procedure, I want to structure it in this way:
ID | food1 | food2 | food3 |
1 | 32 | 12 | 69 |
2 | 22 | 78 | 56 |
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!
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.
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.
It worked, thanks a lot!
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
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.