BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jacksonan123
Lapis Lazuli | Level 10

I have a data set1:

ID TIME PRED      new_study
1       0       0              1
1     0.25    1.1063    1
1    0.5       5.1534    1
2      0           0          1
2    0.25    1.0462     1
2     0.5      4.8104    1
1     0          0            2
1    0.25     1.1063    2
1   0.5         5.1534   2
2    0            0           2
2    0.25     1.0462    2
2    0.5        4.8104   2

and a data set2

id time       ratio
1      0        0
1     0.25     0.937629938
1     0.5       0.925815966
2      0          0
2    0.25      0.991493022
2    0.5          0.991830201

I would like to merge these data sets into a data set final (example below) in which the same ratio values from dataset2 are repeated for each subject in each new_study

 

ID     TIME     PRED      new_study      ratio
1          0          0                 1                  0
1         0.25  1.1063            1                 0.937629938
1           0.5       5.1534       1                 0.925815966
2           0           0               1                 0
2           0.25   1.0462         1                 0.991493022
2          0.5      4.8104         1                  0.991830201
1           0            0              2                  0
1            0.25   1.1063        2                  0.937629938
1           0.5       5.1534       2                  0.925815966
2           0           0              2                   0
2           0.25  1.0462          2                 0.991493022
2            0.5     4.8104       2                  0.991830201

 

I have tried some merges but each time the study and ratios get sorted which is not what I want.  I need the newstudy and ratio values not to be sorted.  I have included my simple merge code.

proc sort data=data1; by id time;run;
proc sort data=data2; by id time; run;


Data final;
merge data1 data2;
by id time;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data have;

input ID TIME PRED      new_study;

datalines;

1       0       0              1

1     0.25    1.1063    1

1    0.5       5.1534    1

2      0           0          1

2    0.25    1.0462     1

2     0.5      4.8104    1

1     0          0            2

1    0.25     1.1063    2

1   0.5         5.1534   2

2    0            0           2

2    0.25     1.0462    2

2    0.5        4.8104   2

;

 

 

data have1;

input id time       ratio;

datalines;

1      0        0

1     0.25     0.937629938

1     0.5       0.925815966

2      0          0

2    0.25      0.991493022

2    0.5          0.991830201

;

 

 

 

data final;

   if _N_ = 1 then do;

      if 0  then do;

       set have;

     set have1;

      end;

      declare hash myhash(dataset:'have1', multidata:'yes' );

     myhash.defineKey('id','time');

     myhash.defineData('ratio');

     myhash.defineDone( );

       end;

set have;

if myhash.find() ne 0 then ratio=0;

run;

 

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

Most simple is hash find method. Is it ok for you to implement hash?

Astounding
PROC Star

So far, you're doing the right thing (although using PROC SQL might simplify the process).  All  you are missing is a final PROC SORT to put the data back into the right order:

 

proc sort data=final;

by new_study id time;

run;

novinosrin
Tourmaline | Level 20

data have;

input ID TIME PRED      new_study;

datalines;

1       0       0              1

1     0.25    1.1063    1

1    0.5       5.1534    1

2      0           0          1

2    0.25    1.0462     1

2     0.5      4.8104    1

1     0          0            2

1    0.25     1.1063    2

1   0.5         5.1534   2

2    0            0           2

2    0.25     1.0462    2

2    0.5        4.8104   2

;

 

 

data have1;

input id time       ratio;

datalines;

1      0        0

1     0.25     0.937629938

1     0.5       0.925815966

2      0          0

2    0.25      0.991493022

2    0.5          0.991830201

;

 

 

 

data final;

   if _N_ = 1 then do;

      if 0  then do;

       set have;

     set have1;

      end;

      declare hash myhash(dataset:'have1', multidata:'yes' );

     myhash.defineKey('id','time');

     myhash.defineData('ratio');

     myhash.defineDone( );

       end;

set have;

if myhash.find() ne 0 then ratio=0;

run;

 

jacksonan123
Lapis Lazuli | Level 10
It did give me the result that I sought except that it only did so for
new_study, new_study 2 was not output.
jacksonan123
Lapis Lazuli | Level 10

I made an error in coding in your response.  When corrected it worked perfectly by listing all of the studies..

 

Thanks for the help.

jacksonan123
Lapis Lazuli | Level 10

It performed the sort as you stated but it only output new_study1 there was no new_study 2 output.

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
  • 6 replies
  • 884 views
  • 0 likes
  • 3 in conversation