DATA Step, Macro, Functions and more

duplicating rows and adding a new variable

Reply
Regular Contributor
Posts: 199

duplicating rows and adding a new variable

I have a list of names (John, Jack, Tom).
I would like to create the following dataset
John 1
John 2
John 3
Jack 1
Jack 2
Jack 3
Tom 1
Tom 2
Tom 3

Any ideas? Thank you.
Super Contributor
Super Contributor
Posts: 3,174

Re: duplicating rows and adding a new variable

Sounds like a SAS examination "prep" question - suggest reviewing the DO/END statement construct in a DATA step, where you would have an outer DO/END with explicit values, and an inner DO/END code piece.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search arguments, this topic / post:

data step programming do loop site:sas.com

do statement documentation site:sas.com
Frequent Contributor
Posts: 101

Re: duplicating rows and adding a new variable

The solution I offer assumes that your list of names comes from another dataset.

** create dataset of names;
data names;
input nm $ @@;
datalines;
John Jack Tom Jill Sue Bob Jennifer Tim George
;
run;


data names2;
length name $ 10;
set names;
*do nm = 'John', 'Jack', 'Tom';
do i = 1 to 3;
name = cat( strip( nm ), ' ', put( i, 1. ) );
output;
end;
*end;
keep name;
run;

If your list doesn't come from another data set and is really short, you can uncomment the DO loop and type the names like I have above. Also comment the SET statement.
Regular Contributor
Posts: 199

Re: duplicating rows and adding a new variable

Posted in reply to SAS_Bigot
Thank you.
This code did the job:
data names2;
set names;
do var = 1 to 3;
nm = cat( strip( name ), ' ', put( var, 1. ) );
output;
end;
keep name var;
run;
Ask a Question
Discussion stats
  • 3 replies
  • 139 views
  • 0 likes
  • 3 in conversation