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

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 

  1. 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;


2017-03-29.png
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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...

 

 

View solution in original post

5 REPLIES 5
Reeza
Super User

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...

 

 

larkjr18
Fluorite | Level 6

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

Reeza
Super User

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

Tom
Super User Tom
Super User

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);

 

Reeza
Super User

@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 

  1. 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;


 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 5 replies
  • 1670 views
  • 1 like
  • 3 in conversation