Help using Base SAS procedures

Concatenation of 2 fields (need to retain Leading Zeros)

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

Concatenation of 2 fields (need to retain Leading Zeros)

Hi

I am trying to concatenate 2 Char fields (a string & a count field) - but, the result is dropping the leading zeros from the count field.

e.g.

Input Parm:

Total record count is: ####

Expected output:

Total record count is: 000000123


The Count field is of 9 bytes.


Here is what I tried.


Count has the value = 123.

OUTRECD has the value  = Total record count is: ####


LENGTH COUNTC $9.;

COUNTC = (PUT(COUNT,Z9.)); 

PUT COUNTC;   --> Here the value is 000000123, which is what I expect 

OUT_RECD = CAT(SCAN(OUT_REC,1,'####'),COUNTC);   --> Result is without leading zeros

OUT_RECD = SCAN(OUT_REC,1,'####') || COUNTC;  --> Same result

How do I retain the leading zeros when concatenating?? Can someone please help? Thanks in Advance.


Accepted Solutions
Solution
‎09-20-2013 02:31 PM
Frequent Contributor
Posts: 129

Re: Concatenation of 2 fields (need to retain Leading Zeros)

Posted in reply to prasanna_sk

I see several issues.

  1. Variable OUT_REC is mis-spelled in the if statement.  So branch is never executed.

         IF SCAN(OUTREC,2,':') = "####" THEN   

2. After correcting mis-spelling the branch is still not executed.

         IF SCAN(OUT_REC,2,':') = "####" THEN 

3. Investigation of value returned from scan is ' ####'; notice the leading blank.

4. Change if statement to following and this will work:

         IF compress(SCAN(OUT_REC,2,':')) = "####" THEN

Some comments

1. I discovered #1 by looking at the log.  It showed that OUTREC was uninitialized, which frequently points to mis-spelling

2. #3 took a little more effort to find that there is a leading blank in the string returned from scan.

     a.  First I noticed code was different bewteen the two lines but was getting the same results.  So I said, "Aha, the first branch may not be exercised.

     b.  I modified your code to have "then do; .... ;end;" and inserted a put statement to show me which branch I was in.

     c.  That verified that only the else branch was executed.

     d.  I then put the string returned from first scan and saw it had a leading blank.

3.  Posting relevant portions of the log is almost always helpful in solving these kinds of issues.  It will let many of the folks on this website quickly see what is happening.  I would have definitely been able to get further in diagnosing the problem without modifying your code to run for me.

4.  I would consider a different configuring the format selection -- perhaps just look at the first word in OUT_rec (Total or Output, in your example).  Using the repeated # sign is cryptic and also difficult to verify correct configuration.  But maybe you can't change this.

HTH

Larry

View solution in original post


All Replies
Super Contributor
Posts: 282

Re: Concatenation of 2 fields (need to retain Leading Zeros)

Posted in reply to prasanna_sk

Hi,

Try adding a length statement before out_recd is used, e.g.:

length out_recd $32;

Regards,

Amir.

Contributor
Posts: 70

Re: Concatenation of 2 fields (need to retain Leading Zeros)

Posted in reply to prasanna_sk

Hi

What was the alue for "OUT_REC" variable?

Yash

Super Contributor
Posts: 282

Re: Concatenation of 2 fields (need to retain Leading Zeros)

Posted in reply to prasanna_sk

Hi,

I've just realised that I can't replicate your initial issue. I tried to reconstruct your code based on the sample given and came up with:

data _null_;

LENGTH COUNTC $9.;

*length out_recd $32;

count=123;

OUT_RECD='Total record count is: ####';

COUNTC = (PUT(COUNT,Z9.));

PUT COUNTC;   /*--> Here the value is 000000123, which is what I expect */

OUT_RECD = CAT(SCAN(OUT_RECD,1,'####'),COUNTC);   /*--> Result is without leading zeros*/

put OUT_RECD=;

OUT_RECD = SCAN(OUT_RECD,1,'####') || COUNTC;  /*--> Same result*/

put OUT_RECD=;

run;

but this code has different issues when run.

And as has pointed out, I assumed OUT_REC was a typo of OUT_RECD

If this is still an issue then post the full data step code that when run shows the issue reported.

Regards,

Amir.

Super Contributor
Posts: 339

Re: Concatenation of 2 fields (need to retain Leading Zeros)

Posted in reply to prasanna_sk

data want;
count=123;
string="Total record count is: ####";
out_recd=scan(string, 1, "####")||put(count, z9.);
put out_recd;

x=put(count, z9.);
test=scan(string, 1, "####")||x;
put test;
run;

Total record count is: 000000123

Total record count is: 000000123

NOTE: The data set WORK.WANT has 1 observations and 5 variables.

NOTE: DATA statement used (Total process time):

real time           0.01 seconds

cpu time            0.00 seconds

both syntax in the example work just fine for me...

can you provide your full code? I don't think its an issue with the length of out_recd as it would be truncated to the right losing 123 before losing zeros. What version of SAS to you use?

Vince

Contributor
Posts: 23

Re: Concatenation of 2 fields (need to retain Leading Zeros)

Posted in reply to Vince28_Statcan

SAS Release 9.3  - and I am using it in Mainframes.

I did try to assign the value in code as done above - and it works fine in that case. But, it is not working in the full code that I posted.

Contributor
Posts: 23

Re: Concatenation of 2 fields (need to retain Leading Zeros)

Posted in reply to prasanna_sk

I did not want to confuse with other logic that were in the code. Here is the code.

SYSOUT record that we are concerned

WER054I  RCD IN     10000, OUT     154416

Input Parm file (there are other rows which are hardcoded values and does not need any substitution)

Total record count is: ####  

Output Records: ###          

CODE:

/* This step extracts counts from SYSOUT from previous SORT step*/

DATA SYSOINF(KEEP=COUNT);                                             

   INFILE I1INPUT TRUNCOVER;                                          

   INPUT @3 SYNCINF $CHAR7. @;                                        

     IF SYNCINF = "WER054I" THEN                                      

        INPUT @1 INREC $CHAR80.;                                      

     ELSE DELETE;                                                     

     TOTAL = PUT((COMPRESS(SCAN(INREC,2,'OUT'))),15.);                

     COUNT  = INPUT(TOTAL,9.);                                        

                                                                      

PROC PRINT DATA=SYSOINF;TITLE "SYSOINF";RUN;        


/* This is the input parm file */

DATA DEF;                                                             

INFILE I1PARM END=EOF;                                                

INPUT @1 OUT_REC $CHAR80.;                                            

                                                                      

PROC SQL;                                                             

CREATE TABLE LOGOUT AS                                               

   SELECT DEF.*,SYSOINF.* FROM DEF,SYSOINF;                           

                                                                      

PROC PRINT DATA=LOGOUT;TITLE "LOGOUT";RUN;                            

                                                                      

DATA _NULL_;                                                          

SET LOGOUT;                                                           

LENGTH OUT_RECD $80.;                                                 

LENGTH COUNTC $9.; COUNTC = (PUT(COUNT,Z9.));                         

PUT COUNTC;                                                           

                                                                      

FILE O1OUTPUT;                                                        

IF SCAN(OUTREC,2,':') = "####" THEN                                   

   OUT_RECD = CAT(SCAN(OUT_REC,1,'####'),COUNTC);            /* counts with zeros */         

ELSE                                                                  

   OUT_RECD = SCAN(OUT_REC,1,'###') || COMPRESS(COUNT);      /* counts without zeros */             

                                                                      

PUT @1   OUT_RECD  $CHAR80.; 


OUTPUT


********************************* Top of Data **********************************

Total record count is: 10000                                                   

Output Records: 10000                                                          

******************************** Bottom of Data ********************************


Output                                       

Solution
‎09-20-2013 02:31 PM
Frequent Contributor
Posts: 129

Re: Concatenation of 2 fields (need to retain Leading Zeros)

Posted in reply to prasanna_sk

I see several issues.

  1. Variable OUT_REC is mis-spelled in the if statement.  So branch is never executed.

         IF SCAN(OUTREC,2,':') = "####" THEN   

2. After correcting mis-spelling the branch is still not executed.

         IF SCAN(OUT_REC,2,':') = "####" THEN 

3. Investigation of value returned from scan is ' ####'; notice the leading blank.

4. Change if statement to following and this will work:

         IF compress(SCAN(OUT_REC,2,':')) = "####" THEN

Some comments

1. I discovered #1 by looking at the log.  It showed that OUTREC was uninitialized, which frequently points to mis-spelling

2. #3 took a little more effort to find that there is a leading blank in the string returned from scan.

     a.  First I noticed code was different bewteen the two lines but was getting the same results.  So I said, "Aha, the first branch may not be exercised.

     b.  I modified your code to have "then do; .... ;end;" and inserted a put statement to show me which branch I was in.

     c.  That verified that only the else branch was executed.

     d.  I then put the string returned from first scan and saw it had a leading blank.

3.  Posting relevant portions of the log is almost always helpful in solving these kinds of issues.  It will let many of the folks on this website quickly see what is happening.  I would have definitely been able to get further in diagnosing the problem without modifying your code to run for me.

4.  I would consider a different configuring the format selection -- perhaps just look at the first word in OUT_rec (Total or Output, in your example).  Using the repeated # sign is cryptic and also difficult to verify correct configuration.  But maybe you can't change this.

HTH

Larry

Contributor
Posts: 23

Re: Concatenation of 2 fields (need to retain Leading Zeros)

Posted in reply to prasanna_sk

Figured out whats wrong. it was my oversight with some typo. Instead of OUTREC it should have been OUT_REC.. :smileysilly:

I also did what Larry suggested above. Thanks for your help and time.

🔒 This topic is solved and locked.

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

Discussion stats
  • 8 replies
  • 2319 views
  • 3 likes
  • 5 in conversation