turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Extracting the first non-zero digit from a numeric...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-29-2017 10:17 PM

Here is my code and I attached the output. Some of the data only extracted 3 digits and then randomly switches to extracting 4 digits?

This is the problem i am solving

- Consider only the first 4 character digits of SSN, find the duplicate groups through By-group processing, and create a SAS data set for these observations. Present the screenshot of the partial SAS LOG, showing the number of observations in this subset of data. Show the output for “corrected SSN” variable using PROC PRINT (OBS=8);”.

data hi.question4;

infile 'C:\Users\larkj\Desktop\STUDENT.DAT';

input ssn 1-9;

SSNc = put(ssn,9.);

drop SSN;

rename SSNc = SSN;

SSN_New=substr(SSNc,1,4);

run;

proc sort data = hi.question4

dupout = SSN_New

nodupkey;

by SSN_NEW;

run;

proc print data=hi.question4;

run;

Accepted Solutions

Solution

03-29-2017
10:57 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to larkjr18

03-29-2017 10:50 PM

Your datastep doesn't make sense. Read SSN as a character from start, don't convert it.

And the subject line doesn't make sense for your question either...

All Replies

Solution

03-29-2017
10:57 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to larkjr18

03-29-2017 10:50 PM

Your datastep doesn't make sense. Read SSN as a character from start, don't convert it.

And the subject line doesn't make sense for your question either...

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

03-29-2017 10:58 PM

That worked great, don't know why I didn't do that before. Thank you

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to larkjr18

03-29-2017 11:06 PM

If you had wanted to keep using the previous code, use Z9 to apply a 9 digit format with leading 0s.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to larkjr18

03-29-2017 11:10 PM

You can read the SSN as a character string.

`input SSNc $9. ;`

You can convert a numeric value to 9 digits with leading zeros using the Z9 format.

`SSNc = put(ssn,Z9.);`

You can get the first 4 digits of a 9 digit number using arithmetic.

`prefix = int(ssn/10000);`

You can get the first 4 characters of a 9 character string using SUBSTR().

`prefixc = substr(SSNc,1,4);`

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to larkjr18

03-30-2017 01:27 AM

larkjr18 wrote:

Here is my code and I attached the output. Some of the data only extracted 3 digits and then randomly switches to extracting 4 digits?

This is the problem i am solving

- Consider only the first 4 character digits of SSN, find the duplicate groups through By-group processing, and create a SAS data set for these observations. Present the screenshot of the partial SAS LOG, showing the number of observations in this subset of data. Show the output for “corrected SSN” variable using PROC PRINT (OBS=8);”.

data hi.question4;

infile 'C:\Users\larkj\Desktop\STUDENT.DAT';

input ssn 1-9;

SSNc = put(ssn,9.);

drop SSN;

rename SSNc = SSN;

SSN_New=substr(SSNc,1,4);

run;

proc sort data = hi.question4

dupout = SSN_New

nodupkey;

by SSN_NEW;

run;

proc print data=hi.question4;

run;