BookmarkSubscribeRSS Feed
madtex99
Calcite | Level 5
data work.combined;
	merge TMP1.HW10Demo (in=inD) TMP2.HW10Baseline (in=inB);
	by Site;
	if inD and inB then Complete=11;
	else if inD and ~inB then Complete=10;
	else Complete=01;
	Age=yrdif(DOB,Entry,'act/act');
	TTime=datdif(Entry,Test,'act/act');
data work.DBF;
	merge work.combined TMP3.HW10Followup;
	FupTime=datdif(Entry,fupdt,'act/act');
data work.SID;
	merge TMP1.HW10Baseline (in=inB) TMP3.HW10Followup (in=inF);
	by Site;
	if inB and inF then keep Site ID fupdt fuptest Test Score;
	else if inB and ~inF then drop fupdt fuptest Test Score;
run;

My last data step I am trying to merge those two sets and only keep variables that have both Baseline and Follow Up data in the new set. I am getting an error that says my variables have never been referenced. Help please!

3 REPLIES 3
Astounding
PROC Star

You wil need to re-think what you are trying to do here.

 

First, in a SAS data set, all variables exist on every observation.  There is no possibility of keeping (or dropping) variables differently from one observation to the next.

 

Second, KEEP or DROP (no matter how and where specified) cannot be executed conditionally.  They cannnot be part of an IF THEN statement.

 

Theoretically possible if it meets your needs:  You can create two output data sets.  You could keep a different set of variables in each output data set.  And you might use the in= variables to determine which observations get output to which data sets.

 

Patrick
Opal | Level 21

To add to what @Astounding wrote. 

 

SAS tables are rectangular and each observation (row) will have the same set of variables (columns). 

 

SAS variables (the program data vector) get defined during the compilation phase of a data step which is another reason why you can't drop them conditionally during the execution phase.

Patrick_0-1698198398956.png

Overview of the DATA Step

 

To understand the distinction between compilation and execution phase is important and will help you with defining program logic. 

Statements like length, attrib, array, retain etc. are all on compilation level and though can't get used conditionally during the execution phase. 

 

Think of the compilation phase as an iteration zero where the SAS compiler scans through your code and prepares everything for execution.

Tom
Super User Tom
Super User

You should get in the habit of including RUN (or QUIT where appropriate) to help the humans that have to read the code.

data combined;
  merge TMP1.HW10Demo (in=inD) TMP2.HW10Baseline (in=inB);
  by Site;
  if inD and inB then Complete=11;
  else if inD and ~inB then Complete=10;
  else Complete=01;
  Age=yrdif(DOB,Entry,'act/act');
  TTime=datdif(Entry,Test,'act/act');
run;

data DBF;
  merge combined TMP3.HW10Followup;
  FupTime=datdif(Entry,fupdt,'act/act');
run;

You could write THREE datasets in the last data step.  That is the only way to conditionally change the number of variables. 

data SID1(keep=Site ID fupdt fuptest Test Score) 
     SID2(drop=fupdt fuptest Test Score)
     SID3
;
  merge TMP1.HW10Baseline (in=inB) TMP3.HW10Followup (in=inF);
  by Site;
  if inB and inF then output sid1 ; 
  else if inB and ~inF then output sid2; 
  else output sid3;
run;

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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