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
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;
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
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;
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;
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.