Obs | id_i | id_m | days | category | days1995 |
1 | . | 3 | 8 | 2 | 8 |
2 | . | 3 | 8 | 12 | 8 |
3 | . | 4 | 5 | 7 | 18 |
4 | . | 4 | 13 | 7 | 18 |
5 | 1 | . | 2 | 1 | 2 |
6 | 1 | . | 108 | 13 | 108 |
7 | 2 | . | 14 | 2 | 14 |
id_i | id_m | category1 | category2 | … | category7 | category13 | |
1 | . | 2 | . | . | . | . | 108 |
. | . | . | . | . | . | . | . |
. | 4 | . | . | . | 18 | . | . |
First is do you know what the maximum number of categories might be?
I think you should go ahead an show all the desired output for that input. There may be some issues around combinations of Id_i and id_m that aren't obvious.
Also, what do you want to have happen with the days variable?
This may get you started. However since you already have a variable named category you cannot name an array the same. There are a number of ways around that but that is a fiddly bit if the logic works. If you expect more values of category change the 13 to the largest integer number.
data want; set work.days_sum; array cat{13}; retain cat1-cat13; by id_i id_m ; if first.id_m then do; call missing(of cat(*)); end; cat[category]=days1995; if last.id_m; run;
Before you go denormalizing your data in order to solve some other problem you haven't told us about, maybe you could tell us what you are really trying to do?
First is do you know what the maximum number of categories might be?
I think you should go ahead an show all the desired output for that input. There may be some issues around combinations of Id_i and id_m that aren't obvious.
Also, what do you want to have happen with the days variable?
This may get you started. However since you already have a variable named category you cannot name an array the same. There are a number of ways around that but that is a fiddly bit if the logic works. If you expect more values of category change the 13 to the largest integer number.
data want; set work.days_sum; array cat{13}; retain cat1-cat13; by id_i id_m ; if first.id_m then do; call missing(of cat(*)); end; cat[category]=days1995; if last.id_m; run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.