BookmarkSubscribeRSS Feed
wil1212
Calcite | Level 5

Hello,

 

I am trying to create a variable that contains a letter and an incremented numeric value (e.g T1, T2, T3,...Tn). Does it have to be in a certain format? I have tired everything I know, but can't seem to figure it out. I am sure it is super simple. Any help would be greatly appreciated.

14 REPLIES 14
Reeza
Super User

You haven't specified how you want it, but this may give you an idea:

 

Data Want;
do i=1 to 100;
Variable_Want=catt('T', put(i, 8. -l));
OUTPUT;
end;
run;

PGStats
Opal | Level 21
Reminder: catt, cats, and catx functions do their own conversions of numbers to strings. So

Variable_Want = cats("T", i);
PG
MikeZdeb
Rhodochrosite | Level 12

Hi, if you mean that T1, T2, etc. are variable values, try this (creates 100 observations, value T1 through T100) ...

 

data want;
length x $4.;
do _n_=1 to 100;
   x = catt('T',_n_);
   output;
end;
run;

 

ps  CAT functions work fine with numeric variables.  The LOG (no nasty messages).  Also, always use a LENGTH statement when creating variables with CATT functions.  Otherwise you get the default length of 200.

 

133 data want;
134 length x $4.;
135 do _n_=1 to 100;
136 x = catt('T',_n_);
137 output;
138 end;
139 run;

NOTE: The data set WORK.WANT has 100 observations and 1 variables.

wil1212
Calcite | Level 5

Thanks so much. Another question, I do not want to set a limit in the do statement. I would like for it to equal the number of observations in my dataset. Can I exclude the "to 100" part?

Reeza
Super User
Remove the loop portion entirely and use the automatic variable _N_.

x = catt('T',_n_);
PGStats
Opal | Level 21

It can be even simpler than that:

 

data want;
set myData;
x = cats("T", _n_);
run;
PG
MikeZdeb
Rhodochrosite | Level 12

Hi, is this what you mean (use a LENGTH statement) ...

 

data want (keep=x name sex);
length x $4.;
set sashelp.class;
x = catt('T',_n_);
run;

 

data set WANT ...

Obs x Name Sex

1 T1 Alfred M
2 T2 Alice F
3 T3 Barbara F
4 T4 Carol F
5 T5 Henry M
6 T6 James M
7 T7 Jane F
8 T8 Janet F
9 T9 Jeffrey M
10 T10 John M
11 T11 Joyce F
12 T12 Judy F
13 T13 Louise F
14 T14 Mary F
15 T15 Philip M
16 T16 Robert M
17 T17 Ronald M
18 T18 Thomas M
19 T19 William M

Steelers_In_DC
Barite | Level 11

 

data want;
set have;
new_field = cat('T',_N_);
run;

hbi
Quartz | Level 8 hbi
Quartz | Level 8

If you want to pad your alphanumeric ID with leading zeros to maintain sorting, you could do something like the snippet below to detect the number of significant integer digits and generate a format out of it. This will avoid sorting output that looks like this: T1, T10, T100, T11, T12, T13, etc. Robot Happy

 

proc sql;
  SELECT LENGTH(CATT(COUNT(*)))                AS num_digits
       , CATS("z", calculated num_digits, ".") AS pad_zero_format
  INTO :num_digits, :pad_zero_format
  FROM sashelp.class;
quit;
%PUT &num_digits &pad_zero_format;


data want (keep=x name sex);
  length x $4.;
  set sashelp.class;
  x = catt('T',PUT(_n_, &pad_zero_format));
run;

 

PGStats
Opal | Level 21

This is a bit overkill Smiley Wink but if you want to go there, the variable length should also be macro driven:

 

proc sql;
  SELECT LENGTH(CATT(COUNT(*)))+1                  AS num_digits
       , CATS("z", calculated num_digits - 1, ".") AS pad_zero_format
  INTO :num_digits, :pad_zero_format
  FROM sashelp.class;
quit;
%PUT &num_digits &pad_zero_format;


data want (keep=x name sex);
  length x $&num_digits.;
  set sashelp.class;
  x = catt('T',PUT(_n_, &pad_zero_format));
run;

proc print data=want noobs; run;
PG
hbi
Quartz | Level 8 hbi
Quartz | Level 8

Yes, a bit overkill Robot Happy ... but cool, nice! 

MikeZdeb
Rhodochrosite | Level 12

Hi.  In the second data step, you are giving the variable X a length of 4.  That means you would use a Z3. format ...

 

data want (keep=x name sex);
length x $4.;
set sashelp.class;
x = catt('T',put(_n_,z3.));
run;

 

If you really want  to figure out whether the format should be Z2., Z3., etc., you could use (less overkill, but still overkill) ...


data want (keep=x name sex);
length x $4.;
set sashelp.class nobs=obs;
x = catt('T',putn(_n_,catt('Z',length(cat(obs)),'.')));
run;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

May I ask why you need to do this?  It seems to me if you need to do calculations on the numeric part, and from your example the rest is just "T", I would personally drop the T completely, and convert the variable to numeric.  Much easier to work with, and if at some point you do need the T then add it on at report time.  No point having a text field just to store a number and a base character?

ballardw
Super User

To add to RW9

 

If the variable is numeric you can use a PICTURE format to attach any prefix character needed when displayed in almost any report procedure output.

proc format ;
picture t low-high = '00000009'   (prefix='T');
run;

data junk;
   input x;
datalines   ;
1
23
345
123456
;

proc print data=junk noobs;
var x;
format x t.;
run;

The format can handle decimals as well if needed.

 

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
  • 14 replies
  • 6736 views
  • 3 likes
  • 8 in conversation