BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
smackerz1988
Pyrite | Level 9

I have two datasets. One is the SV dataset which contains study visit information (VISIT,VISITNUM,VISITDY) for all subjects in a study. The second dataset is a questionnaire study that has all these visit information columns blank. I'm trying to use hash object to populate these columns by subject based on the analysis date (ADT) and study visit date (SVDT) where applicable. Here is an example of a subjects information 

 

SV

USUBJIDVISITNUMVISITVISITDYADT
10011Screening-2829Mar2022
10012Day 1127Apr2022
10014Week 21511May2022
10015Week 42926May2022
10016Week 85722Jun2022
10017Week 128520Jul2022

 

QS

 

USUBJIDVISITNUMVISITVISITDYSVDTQSDY
1001   27Apr20221
1001   28Apr20222
1001   29Apr20223
1001   30Apr20224
1001   01May20225
1001   02May20226
1001   03May20227
1001   04May20228
1001   05May20229
1001   06May202210
1001   07May202211
1001   08May202212
1001   09May202213
1001   10May202214
1001   11May202215
1001   12May202216
1001   13May202217
1001   14May202218
1001   15May202219
1001   16May202220
1001   17May202221
1001   18May202222
1001   19May202223
1001   20May202224
1001   21May202225
1001   22May202226
1001   23May202227
1001   24May202228
1001   25May202229
1001   26May202230
1001   27May202231
1001   28May202232
1001   29May202233
1001   30May202234
1001   01Jun202236
1001   02Jun202237
1001   03Jun202238
1001   04Jun202239
1001   05Jun202240
1001   06Jun202241
1001   07Jun202242
1001   08Jun202243
1001   09Jun202244
1001   10Jun202245
1001   11Jun202246
1001   12Jun202247
1001   13Jun202248
1001   14Jun202249
1001   15Jun202250
1001   16Jun202251
1001   17Jun202252
1001   18Jun202253
1001   19Jun202254
1001   20Jun202255
1001   21Jun202256
1001   22Jun202257
1001   23Jun202258
1001   24Jun202259
1001   25Jun202260
1001   26Jun202261
1001   27Jun202262
1001   28Jun202263
1001   29Jun202264
1001   30Jun202265
1001   01Jul202266
1001   02Jul202267
1001   03Jul202268
1001   04Jul202269
1001   05Jul202270
1001   06Jul202271
1001   07Jul202272
1001   08Jul202273
1001   09Jul202274
1001   10Jul202275
1001   11Jul202276
1001   12Jul202277
1001   13Jul202278
1001   14Jul202279
1001   15Jul202280
1001   16Jul202281
1001   17Jul202282
1001   18Jul202283
1001   19Jul202284
1001   20Jul202285

 

Desired output

 

USUBJIDVISITNUMVISITVISITDYSVDTQSDY
10012Day 1127Apr20221
1001   28Apr20222
1001   29Apr20223
1001   30Apr20224
1001   01May20225
1001   02May20226
1001   03May20227
1001   04May20228
1001   05May20229
1001   06May202210
1001   07May202211
1001   08May202212
1001   09May202213
1001   10May202214
10014Week 21511May202215
1001   12May202216
1001   13May202217
1001   14May202218
1001   15May202219
1001   16May202220
1001   17May202221
1001   18May202222
1001   19May202223
1001   20May202224
1001   21May202225
1001   22May202226
1001   23May202227
1001   24May202228
1001   25May202229
10015Week 42926May202230
1001   27May202231
1001   28May202232
1001   29May202233
1001   30May202234
1001   01Jun202236
1001   02Jun202237
1001   03Jun202238
1001   04Jun202239
1001   05Jun202240
1001   06Jun202241
1001   07Jun202242
1001   08Jun202243
1001   09Jun202244
1001   10Jun202245
1001   11Jun202246
1001   12Jun202247
1001   13Jun202248
1001   14Jun202249
1001   15Jun202250
1001   16Jun202251
1001   17Jun202252
1001   18Jun202253
1001   19Jun202254
1001   20Jun202255
1001   21Jun202256
10016Week 85722Jun202257
1001   23Jun202258
1001   24Jun202259
1001   25Jun202260
1001   26Jun202261
1001   27Jun202262
1001   28Jun202263
1001   29Jun202264
1001   30Jun202265
1001   01Jul202266
1001   02Jul202267
1001   03Jul202268
1001   04Jul202269
1001   05Jul202270
1001   06Jul202271
1001   07Jul202272
1001   08Jul202273
1001   09Jul202274
1001   10Jul202275
1001   11Jul202276
1001   12Jul202277
1001   13Jul202278
1001   14Jul202279
1001   15Jul202280
1001   16Jul202281
1001   17Jul202282
1001   18Jul202283
1001   19Jul202284
10017Week 128520Jul202285

 

 

Here is the code I'm currently using but I feel i need to incorporate the QSDY day variable maybe as the results are not correct

 

data svmerge;
	if 0 then
		set qs sv;

	if _N_ = 1 then
		do;
			declare hash w(dataset:'sv');
			w.defineKey('svdt');
			w.defineData('visitnum', 'visit', 'visitdy');
			w.defineDone();
		end;

	set qs_start;

	if w.find(key:adt) = 0 then
		output;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Try this 

 

data svmerge(drop = rc);

	if _N_ = 1 then do;
			declare hash w(dataset : 'sv(rename = ADT = svdt');
			w.defineKey('USUBJID', 'svdt');
			w.defineData('visitnum', 'visit', 'visitdy');
			w.defineDone();
		end;

	set QS;

	rc = w.find();
run;

View solution in original post

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

Sample data as data steps:

 

Spoiler
data SV;
input USUBJID $ VISITNUM VISIT :$10. VISITDY ADT :date9.;
format ADT date9.;
infile datalines dlm = '|';
datalines;
1001|1|Screening|-28|29Mar2022
1001|2|Day 1    |1  |27Apr2022
1001|4|Week 2   |15 |11May2022
1001|5|Week 4   |29 |26May2022
1001|6|Week 8   |57 |22Jun2022
1001|7|Week 12  |85 |20Jul2022
;

data QS;
input USUBJID $ SVDT :date9. QSDY;
VISITNUM = .;
length VISIT $ 50;
VISIT    = '';	
VISITDY  = .;
datalines;
1001 27Apr2022 1 
1001 28Apr2022 2 
1001 29Apr2022 3 
1001 30Apr2022 4 
1001 01May2022 5 
1001 02May2022 6 
1001 03May2022 7 
1001 04May2022 8 
1001 05May2022 9 
1001 06May2022 10
1001 07May2022 11
1001 08May2022 12
1001 09May2022 13
1001 10May2022 14
1001 11May2022 15
1001 12May2022 16
1001 13May2022 17
1001 14May2022 18
1001 15May2022 19
1001 16May2022 20
1001 17May2022 21
1001 18May2022 22
1001 19May2022 23
1001 20May2022 24
1001 21May2022 25
1001 22May2022 26
1001 23May2022 27
1001 24May2022 28
1001 25May2022 29
1001 26May2022 30
1001 27May2022 31
1001 28May2022 32
1001 29May2022 33
1001 30May2022 34
1001 01Jun2022 36
1001 02Jun2022 37
1001 03Jun2022 38
1001 04Jun2022 39
1001 05Jun2022 40
1001 06Jun2022 41
1001 07Jun2022 42
1001 08Jun2022 43
1001 09Jun2022 44
1001 10Jun2022 45
1001 11Jun2022 46
1001 12Jun2022 47
1001 13Jun2022 48
1001 14Jun2022 49
1001 15Jun2022 50
1001 16Jun2022 51
1001 17Jun2022 52
1001 18Jun2022 53
1001 19Jun2022 54
1001 20Jun2022 55
1001 21Jun2022 56
1001 22Jun2022 57
1001 23Jun2022 58
1001 24Jun2022 59
1001 25Jun2022 60
1001 26Jun2022 61
1001 27Jun2022 62
1001 28Jun2022 63
1001 29Jun2022 64
1001 30Jun2022 65
1001 01Jul2022 66
1001 02Jul2022 67
1001 03Jul2022 68
1001 04Jul2022 69
1001 05Jul2022 70
1001 06Jul2022 71
1001 07Jul2022 72
1001 08Jul2022 73
1001 09Jul2022 74
1001 10Jul2022 75
1001 11Jul2022 76
1001 12Jul2022 77
1001 13Jul2022 78
1001 14Jul2022 79
1001 15Jul2022 80
1001 16Jul2022 81
1001 17Jul2022 82
1001 18Jul2022 83
1001 19Jul2022 84
1001 20Jul2022 85
;

A few things:

 

  1. If the variables as already present in the QS data set, there is not need to prepare the PDV with the 'if 0 then set' statement.
  2. You probably want to have USUBJID as a key in the hash object too.
  3. I'm not sure why you want QSDY to be part of the lookup. Is that equivalent to VISITNUM in the SV data?

 

Anyways, here is how I would do it.

 

data svmerge;

	if _N_ = 1 then do;
			declare hash w(dataset : 'sv(rename = ADT = svdt');
			w.defineKey('USUBJID', 'svdt');
			w.defineData('visitnum', 'visit', 'visitdy');
			w.defineDone();
		end;

	set QS;

	if w.find() = 0;
run;

 

smackerz1988
Pyrite | Level 9
I'm not sure why you want QSDY to be part of the lookup. Is that equivalent to VISITNUM in the SV data?
it is equivalent to VISITDY so ideally I just want the visit columns to be populated where the visitdy matches with qsdy only. For example,  27Apr2022 is day 1 for both visitdy and qsdy
PeterClemmensen
Tourmaline | Level 20

How does my codes result differ from what you want?

smackerz1988
Pyrite | Level 9
I think the only thing is that I need to retain the values that don't match in QS. So if there is no visit data available I still need to keep that record. Is it a case of adding a call missing statement?
PeterClemmensen
Tourmaline | Level 20

I don't understand. If a value does not match in QS, then it is not included in the output data set? Isn't that the whole point?

 

Otherwise, please show an example of you desired result from the posted sample data. 

smackerz1988
Pyrite | Level 9

I've updated the question to include the desired output. Thanks for clarifying. Basically I need records with matching visits to derive a parameter calculation but still need to keep all the records as it contains questionnaire data that happened in between scheduled visits

PeterClemmensen
Tourmaline | Level 20

Try this 

 

data svmerge(drop = rc);

	if _N_ = 1 then do;
			declare hash w(dataset : 'sv(rename = ADT = svdt');
			w.defineKey('USUBJID', 'svdt');
			w.defineData('visitnum', 'visit', 'visitdy');
			w.defineDone();
		end;

	set QS;

	rc = w.find();
run;
smackerz1988
Pyrite | Level 9
Perfect thanks! Plus your other solution I can apply the logic to other situations so that is also appreciated!

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
  • 8 replies
  • 721 views
  • 2 likes
  • 2 in conversation