DATA Step, Macro, Functions and more

add a value in multiple columns

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 84
Accepted Solution

add a value in multiple columns

[ Edited ]

 Hi All,

I have a dataset below:

data want;
  input Group $ Timepoint GM FRO CONT RES;
datalines;
A 1 3 2 5 2
A 2 2 3 3 8
A 1 4 3 5 6
A 2 5 4 3 7
B 1 6 4 3 5
B 2 5 4 3 6
B 1 4 5 2 7

How can I add 5 for GM - RES  if timepoint = 1?

 

Thanks,

L


Accepted Solutions
Solution
‎06-15-2017 04:52 PM
PROC Star
Posts: 768

Re: add a value in multiple columns

data have;
  input Group$ Timepoint GM FRO CONT RES;
datalines;
A 1 3 2 5 2
A 2 2 3 3 8
A 1 4 3 5 6
A 2 5 4 3 7
B 1 6 4 3 5
B 2 5 4 3 6
B 1 4 5 2 7
; 

data want(drop = i);
   set have;
   array vars{*} GM FRO CONT RES;
   if timepoint = 1 then do i = 1 to dim(vars);
      vars[i] = vars[i] + 5;
   end;
run;

View solution in original post


All Replies
SAS Super FREQ
Posts: 709

Re: add a value in multiple columns

Use an array, so you can do the same operation on many variables.

 

see here for some more details https://support.sas.com/resources/papers/97529_Using_Arrays_in_SAS_Programming.pdf

PROC Star
Posts: 326

Re: add a value in multiple columns

By sql you can as follows

proc sql;
create table newwant as 
select group
        ,timepoint
            ,case when Timepoint = 1 then GM*5 else GM end as GM
			,case when Timepoint = 1 then FRO*5 else FRO end as FRO
			,case when Timepoint = 1 then CONT*5 else CONT end as CONT
			,case when Timepoint = 1 then RES*5 else RES end as RES
			from want;
quit;
Occasional Contributor
Posts: 7

Re: add a value in multiple columns

Try this:

 

data wantnew;
  set want;
  array val{4} GM FRO CONT RES;
  if Timepoint=1 then do i=1 to 4;
     val{i}=Val{i}+5;
  end;
  drop i;
run;

Solution
‎06-15-2017 04:52 PM
PROC Star
Posts: 768

Re: add a value in multiple columns

data have;
  input Group$ Timepoint GM FRO CONT RES;
datalines;
A 1 3 2 5 2
A 2 2 3 3 8
A 1 4 3 5 6
A 2 5 4 3 7
B 1 6 4 3 5
B 2 5 4 3 6
B 1 4 5 2 7
; 

data want(drop = i);
   set have;
   array vars{*} GM FRO CONT RES;
   if timepoint = 1 then do i = 1 to dim(vars);
      vars[i] = vars[i] + 5;
   end;
run;
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 171 views
  • 3 likes
  • 5 in conversation