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

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
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;
--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26
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;
--
Paige Miller
ballardw
Super User

@PaigeMiller 

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.

PaigeMiller
Diamond | Level 26

@ballardw wrote:

@PaigeMiller 

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

--
Paige Miller
SteinOk
Calcite | Level 5
Thank you, i am very pleased i was able to learn something new about how SAS handles if statements.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 3230 views
  • 1 like
  • 3 in conversation