BookmarkSubscribeRSS Feed
rtniblett15
Calcite | Level 5

Novice SAS programmer here.

I have data from an electronic animal feeder that I am trying to set up for analysis. I have a variable "entrydate" which signifies the date of the feeding visit, and I want to create a new variable that refers to the date value and assigns a number to it. For example, the first date is 30AUG22, and I want to create a new variable "dayoftest" which relates to the day of the experiment, where 31AUG22 is 0, 01SEP22 is 1, etc. There are many observations in this dataset where an animal could visit the feeder multiple times, thus creating many instances of the same date. 

I created this bit of code as a cheating workaround, but I want something more legitimate:

 

data esf6;
set esf4;
by entrydate notsorted;
dayoftest1= (entrydate-22887);
run;

My biggest hang-up is that I do not know how to "set" a value to a certain value based on a value of another variable.

Thanks in advance!

3 REPLIES 3
MarkusWeick
Barite | Level 11

Hi @rtniblett15,

for me the assigning of values to the new variable "dayoftest1" looks fine.

Do you have any problems with the results? (check für variable types)

"dayoftest" in the text = "dayoftest1" in the code?

Best

Markus

Please keep the community friendly.
Like posts you agree with or like. Mark helpful answers as “accepted solutions”. Generally have a look at https://communities.sas.com/t5/Getting-Started/tkb-p/community_articles
PaigeMiller
Diamond | Level 26

Looks fine to me, too. What is wrong with the code you show?

 

Can you please show us a small sample data set, and also the desired output? Data sets should be working SAS data step code (and not Excel files or screen captures) which you can type in yourself, or follow these instructions.

--
Paige Miller
rtniblett15
Calcite | Level 5

For example, I want a dayoftest variable where 01sep22 is 2, 02sep22 is 3, and so on. I was looking for a way to base the variable off of the value in the entrydate column. The code I generated in my previous post was something that I came up with based on a numerical format and subtracting the different to January 1,1960 since that is the day 0 that SAS uses, but if something happens to this dataset, it could technically be formatted as a date, although I guess this is negligible. I have another question. This dataset is fairly large to my terms (approx 5700 obs) and I want to make a "processed" dataset where wt is averaged without 0's and feed is summed by day to start. There is further processing that needs to be done. I did this by PROC MEANS which accomplished this task, however I wanted to average the feed by day according to the number of animals on a treatment. If I just do an average of feed it bases the average on the feed per animal, so across feeding events per day it could range from .2 to 5 for instance, and the mean is based off that. My overall goal with this dataset is to remove outliers based on predicted values that I calculated from two real weights recorded at the start and end of test; then I want to average the remaining weights by visualid by entrydate and also sum the feed by the same criteria. Here however, the data only shows one visualid, as this trial is over a period of 6 weeks with 19 animals.

 

Obs desc entry exit location GiltID RFID Trt Duration Feed1 Feed2 Wt Controller Station FV NFV entry2 exit2 feed entrydate12345678910
Gestation01JAN60:12:26:4501JAN60:12:26:4512209109.85E14AD8732.0000.000198.414121001SEP22:10:33:3601SEP22:10:48:002.00001SEP2022
Gestation01JAN60:12:26:4601JAN60:12:26:4612209109.85E14AD90.1830.0000.000121001SEP22:18:00:0001SEP22:18:00:000.18301SEP2022
Gestation01JAN60:12:26:4601JAN60:12:26:4612209109.85E14AD12302.8150.000202.382121001SEP22:18:43:1201SEP22:19:12:002.81501SEP2022
Gestation01JAN60:12:26:4601JAN60:12:26:4612209109.85E14AD1370.0000.0000.000120101SEP22:20:52:4801SEP22:20:52:480.00001SEP2022
Gestation01JAN60:12:26:4601JAN60:12:26:4612209109.85E14AD17743.9990.000208.776121002SEP22:06:28:4802SEP22:06:57:363.99902SEP2022
Gestation01JAN60:12:26:4701JAN60:12:26:4712209109.85E14AD370.0000.0000.000120102SEP22:18:43:1202SEP22:18:43:120.00002SEP2022
Gestation01JAN60:12:26:4701JAN60:12:26:4712209109.85E14AD510.0000.0000.000120102SEP22:19:12:0002SEP22:19:12:000.00002SEP2022
Gestation01JAN60:12:26:4701JAN60:12:26:4712209109.85E14AD1430.0000.0000.000120102SEP22:19:55:1202SEP22:19:55:120.00002SEP2022
Gestation01JAN60:12:26:4701JAN60:12:26:4712209109.85E14AD20.0000.0000.000120102SEP22:20:09:3602SEP22:20:09:360.00002SEP2022
Gestation01JAN60:12:26:4701JAN60:12:26:4712209109.85E14AD2150.3660.000210.539121003SEP22:04:19:1203SEP22:04:33:360.36603SEP2022

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
  • 360 views
  • 1 like
  • 3 in conversation