BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
apeeape
Fluorite | Level 6
I am wondering if there is a way to duplicate the row of data when condition met, and change the value of a specific variable.

For example the original data I have is:
Name Sex Income
Amy F 12635
Bob M 13729
Kim F 16438,18973
Zoe F 15307

I want to duplicate the row of data when there is a comma “,” in the income variable, expect the income variable since I want to show that there are 2 different incomes for that person.

This is the expected output:
Name Sex Income
Amy F 12635
Bob M 13729
Kim F 16438
Kim F 18973
Zoe F 15307

I tried to duplicate the data when there is a comma in the income variable but I don’t know how to change the income value.

data updated_income;
Set income;
If findw(income,”,”) then output;
Run;
1 ACCEPTED SOLUTION

Accepted Solutions
r_behata
Barite | Level 11
data have;
input Name $ Sex $ Income :$20.;
cards;
Amy F 12635
Bob M 13729
Kim F 16438,18973
Zoe F 15307
run;


data want;
	set have;
	
	do _n_=1 to countw(Income,',');
			new_Income=scan(Income,_n_,',');
		output;
	end;
run;

View solution in original post

2 REPLIES 2
r_behata
Barite | Level 11
data have;
input Name $ Sex $ Income :$20.;
cards;
Amy F 12635
Bob M 13729
Kim F 16438,18973
Zoe F 15307
run;


data want;
	set have;
	
	do _n_=1 to countw(Income,',');
			new_Income=scan(Income,_n_,',');
		output;
	end;
run;
ballardw
Super User

@apeeape wrote:
I am wondering if there is a way to duplicate the row of data when condition met, and change the value of a specific variable.

For example the original data I have is:
Name Sex Income
Amy F 12635
Bob M 13729
Kim F 16438,18973
Zoe F 15307

I want to duplicate the row of data when there is a comma “,” in the income variable, expect the income variable since I want to show that there are 2 different incomes for that person.

This is the expected output:
Name Sex Income
Amy F 12635
Bob M 13729
Kim F 16438
Kim F 18973
Zoe F 15307

I tried to duplicate the data when there is a comma in the income variable but I don’t know how to change the income value.

data updated_income;
Set income;
If findw(income,”,”) then output;
Run;

One way. The first data step creates a data set similar to what you show. The second extracts the income values into a numeric variable (much more typical for analysis of income).

data have;
  input Name $ Sex $ Income :$12. ;
datalines;
Amy F 12635
Bob M 13729
Kim F 16438,18973
Zoe F 15307
;

data want;
   set have;
   if index(income,',') then do i=1 to countw(income,',');
     incomenum = input(scan(income,i,','),f12.);
     output;
   end;
   else do;
      incomenum= input(income,f12.);
      output;
   end;
   /*drop i income;*/
run;

The explicit OUTPUT statement executes when you want to write to the output data set.

The COUNTW function as shown uses the comma to delimit the values and returns how many values you have. Then we extract each one with the SCAN function and use input to create the numeric variable. I have commented out a drop statement to remove variables that you may not need.

If your really must have the output variable with the income information named Income then

data want (rename=(incomenum = income));
   set have;
   if index(income,',') then do i=1 to countw(income,',');
     incomenum = input(scan(income,i,','),f12.);
     output;
   end;
   else do;
      incomenum= input(income,f12.);
      output;
   end;
   drop i income;
run;

You needed at least one more variable to hold the value of Income while creating a new variable with each piece. There are other ways to this. You could rename the Income variable on the Set statement similar to as done above on the DATA statement.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 2 replies
  • 1329 views
  • 2 likes
  • 3 in conversation