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

Hello,

 

I have a data table that I need to create based off another table.  My original table looks like this:

TEXT LCNT
ABC   5
DEF   2
GHI   12      

I need to turn that into

ABC_1

ABC_2

ABC_3

ABC_4

ABC_5

DEF_1

DEF_2

GHI_1

GHI_2

GHI_3

GHI_4

GHI_5

GHI_6

GHI_7

GHI_8

GHI_9

GHI_10

GHI_11

GHI_12

 

What would be the best approach to loop using a variable value?  I thought of doing a do loop inside a data step but how can I set the variable value to loop through?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

I don't think there's a need for macro variables here.

 

data have;
	input text $ lcnt;
	datalines;
abc 5
def 2
ghi 12
;
run;

data want;
	set have;

	do i=1 to lcnt;
		new_var=catx("_", text, i);
		output;
	end;
	
	keep new_var;
run;

View solution in original post

4 REPLIES 4
PBsas
Obsidian | Level 7

Try this:

data have;

input text $ lcnt;

datalines;

abc 5

def 2

ghi 12

;

run;

 

data _null_;

set have;

call symput("new_cnt",max(lcnt));

run;

 

data want;

set have;

do i=1 to &new_cnt. until(lcnt<=i);

new_var=cats(text,"_",i);

output;

end;

run;

Reeza
Super User

I don't think there's a need for macro variables here.

 

data have;
	input text $ lcnt;
	datalines;
abc 5
def 2
ghi 12
;
run;

data want;
	set have;

	do i=1 to lcnt;
		new_var=catx("_", text, i);
		output;
	end;
	
	keep new_var;
run;
jerry898969
Pyrite | Level 9
Thank you everyone who posted. This solution worked great. Thanks
PGStats
Opal | Level 21
data want;
set have;
do i = 1 to lcnt;
	ID = catx("_", text, lcnt);
	output;
	end;
keep ID;
run;
PG

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 1450 views
  • 2 likes
  • 4 in conversation