BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sophia_SAS
Obsidian | Level 7

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

4 REPLIES 4
Haikuo
Onyx | Level 15

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

sophia_SAS
Obsidian | Level 7

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.

Haikuo
Onyx | Level 15

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;

ballardw
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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