DATA Step, Macro, Functions and more

Replace values in array

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

Replace values in array

Hello.

I've a column vector array 61312*1. Values ranges from 0 to 20. I need to replace the values (0 to 20) with the below mentioned values(0 to 7):

0=0, 1=1, 2,3=2, 4=3, 5,6,13=4, 7,8,9=5, 10,11,12,16=6, 14,15,17,18,19,20=7

Thanks in advance.


Accepted Solutions
Solution
‎07-07-2014 11:34 AM
Trusted Advisor
Posts: 1,230

Re: Replace values in array

Posted in reply to fayyazcivil

If just interested in displaying then you can try proc format.

data have;

do vector=1 to 20;

output;

end;

run;

proc format;

value vector

0=0

1=1

2,3=2

4=3

5,6,13=4

7,8,9=5

10,11,12,16=6

14,15,17,18,19,20=7

;

data want;

set have;

format vector vector.;

run;

View solution in original post


All Replies
Super User
Super User
Posts: 7,992

Re: Replace values in array

Posted in reply to fayyazcivil

Not sure what the column vector array means, but with normal arrays you could use if statements, select/when statements, in fact any conditional logic in a datastep or sql to change the information e.g.:

if the_array{iterator}=0 then 0;

if the_array{iterator}=1 then 1;

...

if the_array{iterator} in (14,15,16,17,18,19,20) then 7;

Super User
Posts: 19,869

Re: Replace values in array

Posted in reply to fayyazcivil

Use Proc Format:

Here's a good reference on how to recode something:

http://www2.sas.com/proceedings/sugi30/001-30.pdf

Solution
‎07-07-2014 11:34 AM
Trusted Advisor
Posts: 1,230

Re: Replace values in array

Posted in reply to fayyazcivil

If just interested in displaying then you can try proc format.

data have;

do vector=1 to 20;

output;

end;

run;

proc format;

value vector

0=0

1=1

2,3=2

4=3

5,6,13=4

7,8,9=5

10,11,12,16=6

14,15,17,18,19,20=7

;

data want;

set have;

format vector vector.;

run;

Super User
Posts: 19,869

Re: Replace values in array

Posted in reply to fayyazcivil

You say an array of 613121*1, which in my head is 60K rows and 1 column? If so this isn't considered an array in SAS. An array in SAS is in a single row, as such, 1*61K values.

Occasional Contributor
Posts: 14

Re: Replace values in array

@Reeza, yes it has 60K rows and I column.

I am sorry I didn't know about that. Now I know what is array in SAS.

Super User
Posts: 19,869

Re: Replace values in array

Posted in reply to fayyazcivil

That's fine, its just that @RW9 code won't work for that, you'll need something closer to stat@sas code.

SAS Learning Module: Creating and recoding variables in SAS

Occasional Contributor
Posts: 14

Re: Replace values in array

I am reading Proc Format, and let's see how it works.

🔒 This topic is solved and locked.

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

Discussion stats
  • 7 replies
  • 274 views
  • 6 likes
  • 4 in conversation