I am trying to create a new dataset based on an existing dataset.
My data contains a unique ID and number of observations (N_OBS) for each unique ID, as shown in the code below.
data have; input ID $ N_OBS; cards; 01 0 02 1 03 3 04 2 ; run;
I am trying to create a row for each unique value connected to the ID, so if ID='03' has 3 observations in N_OBS, i would like 3 rows with the ID '03' counting the number of observations OBS1-OBS3.
The end result i am looking for would be:
data want;
input ID $ OBS_ID $; cards; 02 OBS1 03 OBS1 03 OBS2 03 OBS3 04 OBS1 04 OBS2 ; run;
I am uncertain how to approach this problem, and tips/help would be appreciated.
data have;
input ID $ N_OBS;
length obs_id $ 16;
if n_obs>0 then do i=1 to n_obs;
obs_id=cat('obs',i);
output;
end;
drop i n_obs;
cards;
01 0
02 1
03 3
04 2
;
run;
data have;
input ID $ N_OBS;
length obs_id $ 16;
if n_obs>0 then do i=1 to n_obs;
obs_id=cat('obs',i);
output;
end;
drop i n_obs;
cards;
01 0
02 1
03 3
04 2
;
run;
I may have spent too much time on this forum. My initial idea for a solution varied by exactly 2 characters from yours, basically style choices.
@ballardw wrote:
I may have spent too much time on this forum. My initial idea for a solution varied by exactly 2 characters from yours, basically style choices.
Alternative explanation: great minds think alike.
Alternative explanation 2: birds of a feather flock together
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.