DATA Step, Macro, Functions and more

Increment a Character and Numeric Value

Reply
Occasional Contributor
Posts: 10

Increment a Character and Numeric Value

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.

Super User
Posts: 17,760

Re: Increment a Character and Numeric Value

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;

Respected Advisor
Posts: 4,641

Re: Increment a Character and Numeric Value

Reminder: catt, cats, and catx functions do their own conversions of numbers to strings. So

Variable_Want = cats("T", i);
PG
Valued Guide
Posts: 765

Re: Increment a Character and Numeric Value

[ Edited ]

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.

Occasional Contributor
Posts: 10

Re: Increment a Character and Numeric Value

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?

Super User
Posts: 17,760

Re: Increment a Character and Numeric Value

Remove the loop portion entirely and use the automatic variable _N_.

x = catt('T',_n_);
Respected Advisor
Posts: 4,641

Re: Increment a Character and Numeric Value

It can be even simpler than that:

 

data want;
set myData;
x = cats("T", _n_);
run;
PG
Valued Guide
Posts: 765

Re: Increment a Character and Numeric Value

[ Edited ]

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

Valued Guide
Posts: 858

Re: Increment a Character and Numeric Value

 

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

Contributor hbi
Contributor
Posts: 66

Re: Increment a Character and Numeric Value

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;

 

Respected Advisor
Posts: 4,641

Re: Increment a Character and Numeric Value

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
Contributor hbi
Contributor
Posts: 66

Re: Increment a Character and Numeric Value

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

Valued Guide
Posts: 765

Re: Increment a Character and Numeric Value

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;

Super User
Super User
Posts: 7,392

Re: Increment a Character and Numeric Value

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?

Super User
Posts: 10,473

Re: Increment a Character and Numeric Value

[ Edited ]

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.

 

Ask a Question
Discussion stats
  • 14 replies
  • 866 views
  • 1 like
  • 8 in conversation