I am currently working on a dataset which contains records of daily time use for a large number of participants (American Time Use Survey activity data). I am trying to modify some data before proceeding with the next steps, but my SAS code is not working as intended.
Here’s the condition I am trying to implement in the code:
TUCASEID
) has only two commute periods (TRCODE
in 180501, 180503, 180504), where:TUTIER2CODE=05
) and the other right after work.TUACTDUR24
).TEWHERE
between 12 and 21), but the other is invalid (TEWHERE
less than 12).In this case, I want to replace the invalid modality with the valid modality.
I tried the code below, but the results are different from what I expected. Which parts need to be changed?
Example input data in the form of a working data step for the variables needed. That way we can test code.
Then provide for the example what the expected/desired result is.
Did your log show any interesting messages?
Are all of those code variables actually numeric?
You have a comment about "15 minutes". Are the variables involved SAS TIME or DATEtime variables? Comparisons as written would be looking at 15 second differences. If the values are not SAS time or datetime values then there can be all sorts of issues with how the actual values are stored and what is needed to determine "15 minutes". It may help to provide the output from Proc Contents for your data set Baseline_2023 to clarify any questions about your variables.
Definitely recommend posting a working input dataset as @ballardw suggested. The one thing that jumps out is that you're using LAG() conditionally, which is rarely going to do what you expect. Most of the time, if you're using LAG(), results are much more predictable if you create a variable *un*conditionally (that is, not part of an IF statement / block), then instead use that variable where you're currently using LAG in your code:
lag_val = lag(val);
if x>y then newval=lag_val;
Please show some sample data, in the form of a working DATA step. Then show us what you want the result of that sample to look like.
Help us help you.
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
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.
Ready to level-up your skills? Choose your own adventure.