BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
echoli
Obsidian | Level 7

 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

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
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

4 REPLIES 4
BrunoMueller
SAS Super FREQ

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

kiranv_
Rhodochrosite | Level 12

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;
CatCol
Fluorite | Level 6

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;

PeterClemmensen
Tourmaline | Level 20
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;

sas-innovate-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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