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

Hi All,

I am trying to create below output for variable ADY with attached program but I got something different values. Can anyone please help on this.

Required output:

subjid avisit ady_orig ady
001 1-week 6 0
001 1-month 27 21
001 3-month 90 63 (63 = 90-21+6)
001 6-month 180 90 (90 = 180- 63-21+6)

 

Program Used:

data od;
set od(rename=(ady = ady_orig));
by studyid usubjid avisitn avisit adtm ady_orig epoch alat;
retain ADY;
if first.usubjid then ADY = ADY_ORIG;
else ADY = ADY_ORIG - ADY;
run;

 

Thanks,

Kumar.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

HI @Kumar6  Assuming you have ady_orig  in your dataset and you want to create new variable ady, you likely want to use DIF function.

 

data want;

 set have;

 by id;

ady=dif(ady_orig);

if first.id then ady=.;

run;

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

HI @Kumar6  Assuming you have ady_orig  in your dataset and you want to create new variable ady, you likely want to use DIF function.

 

data want;

 set have;

 by id;

ady=dif(ady_orig);

if first.id then ady=.;

run;

Tom
Super User Tom
Super User

You cannot rename a variable to the name of a variable that already exists.

Which variable is the input and which is the output?

Can you explain the logic of your formula?

Kumar6
Obsidian | Level 7
Sure, Thanks for your advice.

I just re-framed the program and added 1 more subject.

Required output:
subjid avisit ady ady_mod
001 1-week 6 6
001 1-month 27 21
001 3-month 90 63 (63 = 90-21+6)
001 6-month 180 90 (90 = 180- 63-21+6)

002 1-week 8 8
002 1-month 36 28(28= 36-8)
002 3-month 94 58(58=94-36+8)



Program Used:

data od;
set od;
by studyid usubjid avisitn avisit adtm ady epoch alat;
retain ADY_mod;
if first.usubjid then ADY_mod = ADY;
else ADY_mod = ADY - ADY_mod;
run;
ballardw
Super User

Be extremely cautious, as in don't do this with critical data sets until you've worked out any logic issues with a subset or  other data set:

 

Data do;

   set do;

 

This completely replaces the original Do data set if there are no syntax errors that stop the set from running.

Renaming or dropping variables means that if you need to rerun the code for some reason, such as correcting a typo from + to - , can cause errors because the original data is gone and the "old" names are no longer available at that point.

If you make an assignment value error but not a syntax error you no longer have the values you expect.

 

It is much better until you get more experience with SAS to use a different output data set name.

PhilC
Rhodochrosite | Level 12

I would also ask you for more information too.

 

Also please provide more data using at least two SUBJIDs.  Problems in this type of evolution are typical when 1) using lag functions and 2) data boundaries between two SUBJIDs records are crossed.

Kumar6
Obsidian | Level 7

I just re-framed the program and added 1 more subject.
Thank You for your time on this.


Required output:
subjid avisit ady ady_mod
001 1-week 6 6
001 1-month 27 21
001 3-month 90 63 (63 = 90-21+6)
001 6-month 180 90 (90 = 180- 63-21+6)

002 1-week 8 8
002 1-month 36 28(28= 36-8)
002 3-month 94 58(58=94-36+8)



Program Used:

data od;
set od;
by studyid usubjid avisitn avisit adtm ady epoch alat;
retain ADY_mod;
if first.usubjid then ADY_mod = ADY;
else ADY_mod = ADY - ADY_mod;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 853 views
  • 1 like
  • 5 in conversation