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.
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;
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.
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?
It can be even simpler than that:
data want;
set myData;
x = cats("T", _n_);
run;
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
data want;
set have;
new_field = cat('T',_N_);
run;
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.
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;
This is a bit overkill 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;
Yes, a bit overkill ... but cool, nice!
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;
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?
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: