## determining length count for observations within the same variable

Solved
Frequent Contributor
Posts: 114

# determining length count for observations within the same variable

SAS Community-

Using a large dataset, there is the numeric variable -  'SubjectID'.  Within 'SubjectID' the observations including a number of different lengths.  For example some SubjectID observations have 6 numbers while others have 7 or 8 numbers.

SubjectID

234583

5546492

5546489

234577

68907734

68907736

234581

Please advise on a SAS function that will inform me on how many Subject IDs have 6 for a number length, 7th for a number length and 8 for a number length.

Eventually, I will use this information to for a substring.
Thanks!

Accepted Solutions
Solution
‎03-15-2012 08:41 AM
Posts: 3,167

## determining length count for observations within the same variable

Hope this will get you started:

data have;

input SubjectID \$8.;

len=length(subjectid);

cards;

234583

5546492

5546489

234577

68907734

68907736

234581

;

proc freq data=have;

table len;

run;

Regards,

Haikuo

All Replies
Solution
‎03-15-2012 08:41 AM
Posts: 3,167

## determining length count for observations within the same variable

Hope this will get you started:

data have;

input SubjectID \$8.;

len=length(subjectid);

cards;

234583

5546492

5546489

234577

68907734

68907736

234581

;

proc freq data=have;

table len;

run;

Regards,

Haikuo

Frequent Contributor
Posts: 114

## determining length count for observations within the same variable

Haikuo-

One quick follow-up question, how do I do this step without using the input statement?

I want to use a dataset currently located in my work library.  According to proc contents for this dataset, the subject id has the variable listed as numeric with 6 as the length (though I know there are subject ids with up to 9 numbers).

I'm sure this is a very simple SAS 101 question. I just can't seem to get my head around it.

Thanks.

Posts: 3,167

## determining length count for observations within the same variable

/*This to input the numberic value*/

data have;

input SubjectID ;

cards;

234583

5546492

5546489

234577

68907734

68907736

234581

;

data want;

set have;

len=length(strip(subjectid));

/*This to get the occupied length*/

run;

proc freq data=want;

table len;

run;

Super User
Posts: 13,583

## determining length count for observations within the same variable

If the values are numeric the length refers to the number of bytes used to store the value. The range for length of numerics is 3 to 8. This might do what you need.

data len;

len = length (put(SubjectId,f10.0));

run;

proc freq;

table len;

run;

🔒 This topic is solved and locked.