BookmarkSubscribeRSS Feed
octrout
Calcite | Level 5


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;
6 REPLIES 6
octrout
Calcite | Level 5
Thanks for the reply. What if i have many variables to replace values ?
ballardw
Super User

@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 @Kurt_Bremser 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.

octrout
Calcite | Level 5

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;

 

 

Kurt_Bremser
Super User

@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.

Reeza
Super User

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.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1680 views
  • 0 likes
  • 4 in conversation