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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
LarryWorley
Fluorite | Level 6

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

8 REPLIES 8
Amir
PROC Star

Hi,

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

length out_recd $32;

Regards,

Amir.

yaswanthj
Calcite | Level 5

Hi

What was the alue for "OUT_REC" variable?

Yash

Amir
PROC Star

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.

Vince28_Statcan
Quartz | Level 8

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

prasanna_sk
Calcite | Level 5

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.

prasanna_sk
Calcite | Level 5

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                                       

LarryWorley
Fluorite | Level 6

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

prasanna_sk
Calcite | Level 5

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.

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!

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
  • 8 replies
  • 4138 views
  • 3 likes
  • 5 in conversation