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

Hi,

 

i need to modify my input dataset creating new rows for each customer.

 

My input dataset is like this:

 

customer - month - status

111-3-active

222-4-abandoned

333-5-abandoned

 

And i need to create a new dataset like this one:

 

customer - month - status

111 - 1 - active

111- 2 - active

111- 3- active

222- 1 - active

222 - 2 - active

222 - 3 - active

222 - 4 - abandoned

333 - 1 - active

333 - 2 - active

333 - 3 - active

333 - 4 - active

333 - 5 - abandoned

 

How can i do? Thank you in advance.

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

Try this:


data have;
infile cards dlm = "-";
input customer month status $ 12.;
cards;
111-3-active
222-4-abandoned
333-5-abandoned
;
run;
proc print;
run;

data want;
  set have;
  do m = 1 to month;
    if m = month then s = status;
                 else s = 'active';
    output;
  end;
  drop month status;
  rename 
  m = month; 
  s = status;
run;
proc print;
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

9 REPLIES 9
Tiri6
Fluorite | Level 6

Hi,

 

i need to modify my input dataset in order to create a kind of log of customer's actions.

 

My starting dataset is:

 

customer - churn month - status

111         -        3             -  abandoned

222        -        5             -  active

333        -        4             -  abandoned

 

I need to create a dataset like this:

 

customer - churn month - status      

111          -           1         - active

111          -           2         - active

111          -           3         - abandoned

222        -            1         - active

222        -            2         - active

222        -            3         - active

222        -            4         - active

222        -            5         - active

333        -            1         - active

333        -            2         - active

333        -            3         - active

333        -            4         - abandoned

 

Could you please help me? 

Thank you in advance

PaigeMiller
Diamond | Level 26

In this problem, are the months always numbered 1 to a maximum of 5, or can there be months greater than 5? If greater than 5, how big can the month numbers go? Or is the program supposed to figure this out from the data?

 

Do the months always start at 1 for a customer?

--
Paige Miller
Tiri6
Fluorite | Level 6

I don't have a maximum number, months could ideally go from 1 to 999

PaigeMiller
Diamond | Level 26

@Tiri6 please don't post questions more than once. Your question is already answered in your other thread at https://communities.sas.com/t5/SAS-Programming/How-to-add-rows/m-p/742353#M232185

 

--
Paige Miller
Tiri6
Fluorite | Level 6
Yes i know but at first this thread was marked as spam and it wasn't published, i don't know why.
yabwon
Onyx | Level 15

Try this:


data have;
infile cards dlm = "-";
input customer month status $ 12.;
cards;
111-3-active
222-4-abandoned
333-5-abandoned
;
run;
proc print;
run;

data want;
  set have;
  do m = 1 to month;
    if m = month then s = status;
                 else s = 'active';
    output;
  end;
  drop month status;
  rename 
  m = month; 
  s = status;
run;
proc print;
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Kurt_Bremser
Super User

See this:

data have;
infile datalines dlm="-";
input customer $ month status :$10.;
datalines;
111-3-active
222-4-abandoned
333-5-abandoned
;

data want (rename=(_status=status));
set have;
length _status $10;
_status = "active";
do month = 1 to month - 1;
  output;
end;
_status = status;
output;
drop status;
run;
sonofendor
Fluorite | Level 6

 

Hi! You can create multiple observations in output dataset from each input line with OUTPUT statement. More on this here: OUTPUT Statement 

In your example you can do that in a DO loop.

data work.input_set;
	input customer $ month status $15.;
	datalines;
111 3 active
222 4 abandoned
333 5 abandoned
;
run;

data work.new_set;
	set work.input_set;
	current_month = month;
	current_status = status;
	do month = 1 to current_month;
		if month < current_month then status = 'active';
			else status = current_status;
		output;
	end;
	drop current_status current_month;
run;

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 9 replies
  • 1278 views
  • 4 likes
  • 5 in conversation