Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pushing values into an Array 1

Status
Not open for further replies.

PerlIsGood

Programmer
Jan 24, 2002
154
US
Ok, I'm just starting to pick up SAS for my employer and I have my first project I'm trying to sort out. I'm pulling data from a database where the field is a 10-bit code (i.e., 0010000000). Each bit set stands for a different error code (i.e., previous example stands for a P1 error).

Writing code to catch that and place human-readable values in a field is easy, what's killin' me is figuring out how to capure a field that has multiple bits set (i.e. 1000100100). This is what I have so far:

Code:
data test (drop=PBIT1 PBIT2 PBIT3 PBIT4 PBIT5 PBIT6 PBIT7 PBIT8 PBIT9 PBIT10 i);
	set PaymentErrors;
	length PE_CODE $ 20.;
	array PBIT (10) $ ('P8' 'No Errors' 'P1' 'P2' 'P3' 'P4' 'P5' 'P6' 'P7' 'P9');
	do i = 1 to 10;
		if substr(PROCESSING_ERRORS,i,1) = 1 then PE_CODE = PVH || PBIT(i);
	end;
run;

This 'almost' works. At "PE_CODE = PHV || PBIT(i)", it only stores the value from the final loop iteration. I'm assuming that the loop is destroying the PHV (placeholder) variable at every iteration. I thought using VB-esque syntax would fix it, "PE_CODE = PE_CODE || PBIT(i)", but then my PE_CODE variable is blank...

The desired end result is to have multiple values (P1, P2, etc.) 'pushed' into the PE_CODE variable for any field that had multiple bits set.

Any ideas?

Notorious P.I.G.
 
PerlIsGood,
While I wouldn't do your task the way you describe, I think that your code needs the 'output' statement in your 'do' loop.

You also need to 'push' the values into the PE_CODE var.
So you need to add to your statement:
do i = 1 to 10;
PE_CODE = trim(PE_CODE)||' '||PVH || PBIT(i);
output;
end;
This should give you enough to chew on.

If you need some more info let me know.
(What is so good about Perl? :)
Klaz
 
Thanks for the reply. I gave your addition a shot and ended up with quite a few duplicate rows, and the PE_CODE variable still only held the last value read instead of all positive values read.

You mentioned that this wasn't how you'd do it - what did you have in mind?

(What is so good about Perl? :)
It's been so long since I coded any decent length of Perl, I don't remember - [morning]

Notorious P.I.G.
 
How about this way:
Code:
proc format;
  value err
   1 = 'P8'
   2 = 'No Errors'
   3 = 'P1'
   4 = 'P2'
   5 = 'P3'
   6 = 'P4'
   7 = 'P5'
   8 = 'P6'
   9 = 'P7'
   10= 'P9'
   ;
 run;
   
data test;
  length errormsg $50;
  x="0110000010";
  do i=1 to 10;
    if input(substr(x,i,1),8.) eq 1 then do;
    msg = trim(left(put(i,err.)));
    errormsg=trim(errormsg)||' '||trim(msg);
  end;    
  end;
  run;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top