DATA Step, Macro, Functions and more

replacing value using array or macro

Reply
Occasional Contributor
Posts: 6

replacing value using array or macro


Hi, I would like to replace values in C and D with value from B. Can anyone help to create code using array or macro ? Thank you so much !

data have;
input A $ B C $	D ;
datalines;
CA	1	1	1
CA	2	B	1
CA	3	B	9
CA	4	B	8
CA	6	R	2
NV	1	B	1
NV	2	B	1
NV	3	B	1
NV	4	B	1
NV	5	B	1
NV	6	B	1
NV	1	R	4
NV	2	R	6
;
run;
Super User
Posts: 7,854

Re: replacing value using array or macro

Neither macro nor arrays are needed.

data want;
set have;
c = put(b,best.);
d = b;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 6

Re: replacing value using array or macro

Thanks for the reply. What if i have many variables to replace values ?
Super User
Posts: 11,343

Re: replacing value using array or macro


octrout wrote:
Thanks for the reply. What if i have many variables to replace values ?

Provide a more complete example. It is likely to be a variation on what @KurtBremser has already provided.

 

Macro is likely not the way to go though a number of arrays may be needed depending on your rules for which values are used to assign to other variables.

Occasional Contributor
Posts: 6

Re: replacing value using array or macro

Hi, 

 

I have about 60 variables that need to replace with values from one variable.

This would be an example data set need to replace c,d,e,f,g with values from B.  Thanks ! 

data have;
input A $ B C $	D E F G ;
datalines;
CA	1	1	1 20 7 10
CA	2	B	1 2 5 18
CA	3	B	9 3 5 18
CA	4	B	8 3 3 7
CA	6	R	2 4 2 6
NV	1	B	1 4 1 9
NV	2	B	1 4 1 10
NV	3	B	1 5 7 18
NV	4	B	1 5 4 8
NV	5	B	1 5 4 7
NV	6	B	1 5 6 8
NV	1	R	4 5 7 9
NV	2	R	6 5 8 10
;
run;

 

 

Super User
Posts: 7,854

Re: replacing value using array or macro


octrout wrote:

Hi, 

 

I have about 60 variables that need to replace with values from one variable.

This would be an example data set need to replace c,d,e,f,g with values from B.  Thanks ! 

data have;
input A $ B C $	D E F G ;
datalines;
CA	1	1	1 20 7 10
CA	2	B	1 2 5 18
CA	3	B	9 3 5 18
CA	4	B	8 3 3 7
CA	6	R	2 4 2 6
NV	1	B	1 4 1 9
NV	2	B	1 4 1 10
NV	3	B	1 5 7 18
NV	4	B	1 5 4 8
NV	5	B	1 5 4 7
NV	6	B	1 5 6 8
NV	1	R	4 5 7 9
NV	2	R	6 5 8 10
;
run;

 

 


Declare one array for character and another for numeric variables. Then use my assignments in do loops for 1 to dim(array), one for the numeric and one for the character array.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 19,869

Re: replacing value using array or macro

Then you need to clarify your logic. You can create an array to loop through multiple variables if needed but given what you've posted there's no need for such a solution.

Ask a Question
Discussion stats
  • 6 replies
  • 126 views
  • 0 likes
  • 4 in conversation