BookmarkSubscribeRSS Feed
Kp1234
Fluorite | Level 6

My dataset looks like this

Variable image

And I want it to look like this

 Subject Code  site          subj
 0156 00062    156            62 
 0156 00062    156            62 
 0047 00032    47             32
 0034 00066    34             66
 0032 00029    32             29 
 .
 .  
  • My Code: if "Subject Code"n ^="" then site=input(scan("Subject Code"n,1,' '),z9.);
    put site=;
    if "Subject Code"n ^="" thensubj=input(strip(substr((scan("Subject Code"n,-1)),1,4)),$4.);
    put subj=;

The Output i get:

site=15600062

subj=1560

As you can see sas takes out the leading 0 values and the space " ", because of which it's difficult to split.

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

One way

 

data have;
input subjectcode $&10.;
datalines;
0156 00062
0156 00062
0047 00032
0034 00066
0032 00029
;

data want;
   set have;
   site=prxchange('s/0*([1-9]+) 0*([1-9]+)/$1/', -1, subjectcode);
   subj=prxchange('s/0*([1-9]+) 0*([1-9]+)/$2/', -1, subjectcode);
run;
Kp1234
Fluorite | Level 6

Thank you for your reply, 

I'm using the proc import to read a text file which contains the variable 

 

PROC IMPORT OUT=manifest
datafile="../../sd/accumulative/manifests/manifestcomb.txt"
REPLACE ;
delimiter='09'x;
guessingrows=32767;
RUN;
quit;

 

when sas reads the variable "Study Code"n (This is one variable), it takes out space and the leading 0's Eg: 

0156 00062

becomes: 15600062,

is there something that i need to do in the PROC import to keep the formatting?

 

Tom
Super User Tom
Super User

@Kp1234 wrote:

Thank you for your reply, 

I'm using the proc import to read a text file which contains the variable 

 

PROC IMPORT OUT=manifest
datafile="../../sd/accumulative/manifests/manifestcomb.txt"
REPLACE ;
delimiter='09'x;
guessingrows=32767;
RUN;
quit;

 

when sas reads the variable "Study Code"n (This is one variable), it takes out space and the leading 0's Eg: 

0156 00062

becomes: 15600062,

is there something that i need to do in the PROC import to keep the formatting?

 


Using PROC IMPORT will introduce some inconsistency in how the variables are defined since it can only guess what you want.  But it will NOT remove an embedded spaces.  Sounds like your text file does not have spaces between the two parts of the field. And since that makes it look like a numeric value PROC IMPORT will make a numeric variable instead of a character variable.

 

The real fix is to just write your own program to read the text file instead of using PROC IMPORT.  That will also allow you to fix the goofy variable name with spaces in the middle of it that was used in the code in the original question.

 

If you already have a numeric variable and you want it to print using nine digits then use the Z9. format. 

format subject_code z9.;

Or use the Z9. format with a PUT() function call to make a character variable.

subject_code_string=put(subject_code,z9.);

If you have read actual read the two separate values as individual numeric variables and want to generate the 9 digit string then use the PUT() function on each number separately.

subject_code=put(site,z4.)||put(subj,Z5.);
ed_sas_member
Meteorite | Level 14

Hi @Kp1234 

 

You can use the compress function to remove the zeros :

data have;
	infile datalines truncover;
	input "Subject Code"n $10.;
	datalines;
0156 00062
0156 00062
0047 00032
0034 00066
0032 00029
;
run;

data want;
	set have;
	if "Subject Code"n ^="" then do;
		site = input(compress(scan("Subject Code"n,1,' '),"0"),best.);
		subj = input(compress(scan("Subject Code"n,-1),"0"),best.);
	end;
run;
Tom
Super User Tom
Super User

Does the source variable always have 4 digits, space and then 5 digits?  Is then just use SUBSTR() to split the field.

site=substr(subject_code,1,4);
subj=substr(subject_code,6,5);

Or are you trying to create numeric variables instead of character variables?  Numeric variables do not have any concept of leading zeros.  But you can attach a format that will make the values print with leading zeros.

site=input(subject_code,4.);
subj=input(substr(subject_code,6),5.);
format site z4. subj z5.;

 

 

novinosrin
Tourmaline | Level 20

 data have;
 input Subject_Code $12.;
 cards;
 0156 00062    156            62 
 0156 00062    156            62 
 0047 00032    47             32
 0034 00066    34             66
 0032 00029    32             29 
 ;

data want;
  set have;
  length subj site $5;
  subj=substr(scan(subject_code,1),verify(scan(subject_code,1),'0'));
  site=substr(scan(subject_code,-1),verify(scan(subject_code,-1),'0'));
run;
Kp1234
Fluorite | Level 6

thank you for all your reply, much appreciated.

 

I'm using the proc import to read a text file which contains the variable 

 

PROC IMPORT OUT=manifest
datafile="../../sd/accumulative/manifests/manifestcomb.txt"
REPLACE ;
delimiter='09'x;
guessingrows=32767;
RUN;
quit;

 

when sas reads the variable "Study Code"n (This is one variable), it takes out space and the leading 0's Eg: 

0156 00062

becomes: 15600062,

is there something that I need to do in the PROC import to keep the formatting?

 

ghosh
Barite | Level 11

You're on the right track.  These are nominal numbers, so keep them as alpha 

data have;
	length site subj $5;
	infile datalines;
	input @1 "Subject Code"n $11.;

	if "Subject Code"n ^="" then
		site=scan("Subject Code"n, 1);
	put site=;

	if "Subject Code"n ^="" then
		subj=scan("Subject Code"n, -1);
	put subj=;
	datalines;
 0156 00062    
 0156 00062    
 0047 00032    
 0034 00066    
 0032 00029   
 
 
;
proc print;
run;

Annotation 2019-12-20 144523.png

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 8 replies
  • 1043 views
  • 0 likes
  • 6 in conversation