Help using Base SAS procedures

determining length count for observations within the same variable

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 104
Accepted Solution

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
Respected Advisor
Posts: 3,124

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

View solution in original post


All Replies
Solution
‎03-15-2012 08:41 AM
Respected Advisor
Posts: 3,124

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: 104

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.

Respected Advisor
Posts: 3,124

determining length count for observations within the same variable

I hope the following will help you with your problem:

/*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: 10,497

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;

     set <your dataset>;

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

run;

proc freq;

table len;

run;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 238 views
  • 6 likes
  • 3 in conversation