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 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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