BookmarkSubscribeRSS Feed
DanniekvLith
Calcite | Level 5

Dear sas users,

 

For my university research I am doing my statistics in sas but I now have a question. the dataset is about animals

I added data of death date and reseasons (afvoerdatum afvoerreden) to my dataset with the following code:

proc sort data=thesis.afvoerdatumreden ; 
by IR;
run;

proc sort data=thesis1.geitcompleet_TOTAAL;
by IR;
run;

data thesis1.geitcompleet_TOTAAL1;
merge thesis1.Geitcompleet_totaal thesis.afvoerdatumreden;
by IR;
if dgn =. then delete;

run;



  Now I want to delete the data that I added from certain rows, so i want to delete these data if an animal has an tlt in the dataset. However the following does not work. What is a possible way to do this?

 

data thesis1.geitcompleet_totaal1;
set thesis1.geitcompleet_totaal1;
if tlt ne . then
run;

 

kind regards 

6 REPLIES 6
PaigeMiller
Diamond | Level 26

When code doesn't work, SHOW US the log (the entire log for this PROC, all of it, every single line for this PROC, including code, ERRORs, WARNINGs and NOTEs).

 

Please use the </> icon, paste your log as text into the window that appears.

Insert Log Icon in SAS Communities.png

--
Paige Miller
DanniekvLith
Calcite | Level 5

This is my log:

 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 68         
 69         proc sort data=thesis.afvoerdatumreden ; *sorteren voor mergen;
 70         by IR;
 71         run;
 
 NOTE: Input data set is already sorted, no sorting done.
 NOTE: PROCEDURE SORT used (Total process time):
       real time           0.00 seconds
       user cpu time       0.00 seconds
       system cpu time     0.00 seconds
       memory              696.09k
       OS Memory           31564.00k
       Timestamp           06-01-2022 11:51:46 AM
       Step Count                        325  Switch Count  0
       Page Faults                       0
       Page Reclaims                     50
       Page Swaps                        0
       Voluntary Context Switches        5
       Involuntary Context Switches      0
       Block Input Operations            0
       Block Output Operations           0
       
 
 72         
 73         proc sort data=thesis1.geitcompleet_TOTAAL; *sorteren voor mergen;
 74         by IR;
 75         run;
 
 NOTE: Input data set is already sorted, no sorting done.
 NOTE: PROCEDURE SORT used (Total process time):
       real time           0.00 seconds
       user cpu time       0.00 seconds
       system cpu time     0.00 seconds
       memory              1942.96k
       OS Memory           32588.00k
       Timestamp           06-01-2022 11:51:46 AM
       Step Count                        326  Switch Count  0
       Page Faults                       0
       Page Reclaims                     276
       Page Swaps                        0
       Voluntary Context Switches        6
       Involuntary Context Switches      0
       Block Input Operations            0
       Block Output Operations           0
       
 
 76         
 77         data thesis1.geitcompleet_TOTAAL1;
 78         merge thesis1.Geitcompleet_totaal thesis.afvoerdatumreden; *toevoegen afvoerdatum, reden en sterftedatum en reden -
 78       ! altijd checken of het goed is gegaan en of aantal regels klopt;
 79         by IR;
 80         if dgn =. then delete; * geiten met alleen afvoerreden ect. (dus geen info over pariteit, lamdatum melkgegevens ect. )
 80       ! verwijderen;
 81         
 82         run;
 
 NOTE: There were 29260 observations read from the data set THESIS1.GEITCOMPLEET_TOTAAL.
 NOTE: There were 1965 observations read from the data set THESIS.AFVOERDATUMREDEN.
 NOTE: The data set THESIS1.GEITCOMPLEET_TOTAAL1 has 29260 observations and 27 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.07 seconds
       user cpu time       0.01 seconds
       system cpu time     0.03 seconds
       memory              4318.75k
       OS Memory           35668.00k
       Timestamp           06-01-2022 11:51:46 AM
       Step Count                        327  Switch Count  5
       Page Faults                       0
       Page Reclaims                     708
       Page Swaps                        0
       Voluntary Context Switches        212
       Involuntary Context Switches      0
       Block Input Operations            0
       Block Output Operations           24840
       
 
 83         
 84         data thesis1.geitcompleet_totaal1;
 85         set thesis1.geitcompleet_totaal1;
 86         if tlt ne . then  delete sterftereden sterftedatum;
                                      ____________
                                      79
                                      76
 ERROR 79-322: Expecting a ;.
 
 ERROR 76-322: Syntax error, statement will be ignored.
 
 87         run;
 
 NOTE: The SAS System stopped processing this step because of errors.
 WARNING: The data set THESIS1.GEITCOMPLEET_TOTAAL1 may be incomplete.  When this step was stopped there were 0 observations and 27 
          variables.
 WARNING: Data set THESIS1.GEITCOMPLEET_TOTAAL1 was not replaced because this step was stopped.
 NOTE: DATA statement used (Total process time):
       real time           0.01 seconds
       user cpu time       0.00 seconds
       system cpu time     0.00 seconds
       memory              2466.84k
       OS Memory           32848.00k
       Timestamp           06-01-2022 11:51:46 AM
       Step Count                        328  Switch Count  0
       Page Faults                       0
       Page Reclaims                     319
       Page Swaps                        0
       Voluntary Context Switches        35
       Involuntary Context Switches      0
       Block Input Operations            288
       Block Output Operations           8
       
 
 88         
 89         OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 99         

I now see that in my first post I forgot what was in my code that I wanted to delete, that was as folowing, so I want to delete the sterftereden and sterftedatum (which are the death reasons and dates) if there is a tlt:

data thesis1.geitcompleet_totaal1;
	set thesis1.geitcompleet_totaal1;
	if tlt ne . then  delete sterftereden sterftedatum;
run;

PaigeMiller
Diamond | Level 26

Hello, @DanniekvLith , the code in your log is not the code presented in your original question. From now on, please present the log of the code that is causing problems in your first post. This will help you get faster and better answers.

 

You can't add variable names into a DELETE statement. DELETE causes the entire observation to be deleted. Is that what you want? Or do you want something else?

 

 

--
Paige Miller
Tom
Super User Tom
Super User

DELETE is for removing observations (rows).

DROP is for removing variables (columns).

 

Do you want to drop those variables from the dataset?  If so that cannot be done conditionally the dataset is always rectangular with the same number of variables on every observation.

 

Do you want to set those variables' values to missing?

if tlt ne . then  call missing(sterftereden,sterftedatum);
ballardw
Super User

If you want this bit of code to make sure that two variables are not used for analysis conditionally

data thesis1.geitcompleet_totaal1;
	set thesis1.geitcompleet_totaal1;
	if tlt ne . then  delete sterftereden sterftedatum;
run;  

 then the proper code is likely

data thesis1.geitcompleet_totaal1;
	set thesis1.geitcompleet_totaal1;
	if tlt ne . then call missing (sterftereden, sterftedatum);
run;

 Call Missing will set the values of the variables to missing and will by default be excluded from analysis. SAS will not allow you to remove Variables conditionally. The variable is either in the data set or not. If you want the variable values to be available for TLT when missing this is the approach:

Warning: Use of the same data set on the Set and Data statements such as

 

data somename;
    set somename;
   <any other code>
run;

completely rewrites the data set which means that a logic error could remove/replace values you don't want to change. At this point in your experience I strongly recommend that your data step always creates a new data set.

 

 

data new;
   set somename;
   <other code>
run;

That way when you make a minor coding mistake you do not have to go back to step one. This does not mean that you need to make a new set for each change. You could change the dummy code above to:

data new;
   set somename;
   <other code>
   <added code>
run;

which preserves the original Somename data set and generates different versions of New.

 

 

maguiremq
SAS Super FREQ

First, is this code even executing? You're not telling SAS what to do after the `then` statement. Maybe it executes (I've never written it like this, so I really don't know), but you could certainly omit the `then` and just have this:

 

data thesis1.geitcompleet_totaal1;
	set thesis1.geitcompleet_totaal1;
	if tlt ne .;
run;

But I always find truncated/shortened code a bit more difficult to read, so I would put this:

 

data thesis1.geitcompleet_totaal1;
	set thesis1.geitcompleet_totaal1;
	if tlt eq . then delete;
run;

Second, are you certain that your `tlt` variable is numeric? I prefer to use the MISSING function since it is dynamic and captures missing values regardless of the type.

 

Some documentation: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lrcon/p175x77t7k6kggn1io94yedqagl3.htm

 

data thesis1.geitcompleet_totaal1;
	set thesis1.geitcompleet_totaal1;
	if missing(tlt) then delete;
run;

Third, it's bad practice to write over the same data set (using the same data set in the SET and DATA statements). I would encourage you to increment your numbers like this:

 

data thesis1.geitcompleet_totaal2; /* Here */
	set thesis1.geitcompleet_totaal1;
	if missing(tlt) then delete;
run;

 Edit: I saw your reply to @PaigeMiller -- what are you trying to do with your DELETE statement? Your error is here in your log:

 

 83         
 84         data thesis1.geitcompleet_totaal1;
 85         set thesis1.geitcompleet_totaal1;
 86         if tlt ne . then  delete sterftereden sterftedatum;
                                      ____________
                                      79
                                      76
 ERROR 79-322: Expecting a ;.
 
 ERROR 76-322: Syntax error, statement will be ignored.

Are those variables in your file? If so, you need to use the DROP statement instead, but I'm not really sure what they are. Pardon me if I missed it in your original post.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 3174 views
  • 1 like
  • 5 in conversation