I need to create "variable_want" base on "variable_have" by each "id" and with "year" sorted ascending as shown in the table below.
variable_have has 4 values (missing, 0, 1, -9999) and variable_want should be created as shown below. I would highly appreciate if anyone can help.
Best,
Le
id | Year | variable_have | variable_want |
X4T39JHGK | 2012 | 1 | 1 |
X4T39JHGK | 2013 | 0 | 1 |
X4T39JHGK | 2014 | 1 | |
X4T39JHGK | 2015 | -9999 | 1 |
X4T39JHGK | 2016 | 1 | 1 |
EZQA42HWD | 2015 | 0 | 0 |
EZQA42HWD | 2016 | 0 | 0 |
EZQA42HWD | 2017 | 0 | 0 |
EZQA42HWD | 2018 | 1 | 1 |
EZQA42HWD | 2019 | 0 | 1 |
EZQA42HWD | 2020 | 1 | |
EZQA42HWD | 2021 | -9999 | 1 |
APJV74TMX | 2015 | -9999 | -9999 |
APJV74TMX | 2016 | 0 | -9999 |
APJV74TMX | 2017 | 0 | -9999 |
APJV74TMX | 2018 | 1 | 1 |
APJV74TMX | 2019 | 0 | 1 |
APJV74TMX | 2020 | 1 | |
APJV74TMX | 2021 | -9999 | 1 |
CRMGKSEZH | 2015 | ||
CRMGKSEZH | 2016 | ||
CRMGKSEZH | 2017 | 0 | 0 |
CRMGKSEZH | 2018 | -9999 | -9999 |
CRMGKSEZH | 2019 | 0 | -9999 |
CRMGKSEZH | 2020 | -9999 | |
CRMGKSEZH | 2021 | 1 | 1 |
OK! Excellent! 👍👍
If you would, please mark the solution so that this topic shows as "solved."
Thanks for hanging in there with me,
Jim
That shouldn't be overly difficult. Have you tried using a Retained variable to hold (or the LAG function) the prior value and then just replace the existing value if and only if the current value is greater than the prior value?
Jim
What is the explicit rule that makes the variable_want value for APJV74TMX and CRMGKSEZH stop being -9999 and switch to 1?
Programs implement rules. An example is insufficient without the rule(s) involved.
I could guess at least two different rules but you know what you want. So tell us the rule you are using to make the switches.
@binhle50 wrote:
And Jim, only switch -9999 to 1 for all following year, not previous years. I got everything worked except this. Thanks so much for your help!
Isn't that the behavior of the program now? I must not be understanding.
In the below, in which line numbers would the value of Variable_Want change? And what value would Variable_Want change to?
Jim
Thank you.
What is the difference between Line 4 and Line 12? In Line 4 we want "1", but in Line 12, we want -9999. Is it because for X4T39JHGK there is additional data whereas for APJV74TMX there is not?
In other words, is this what you want?
Jim
@binhle50 wrote:
I want variable_want=1 for all lines 4,12,19.
OK, so like this, yes?
The code I posted should already be doing this. Is it not working for you? If it is not working, did you change the program? If so, I would need to see the actual code that you are running in order to help you. I would also want to see the log.
Is the data you are running different? If so, can you post the data here that is giving you the incorrect values?
Jim
Would it be possible for you to use the SAS code icon when you post your program? It is very hard to read otherwise.
Jim
First: Are all those versions of the array found in cc_iv_sv? I suspect only Dyn is. If so, it's probably best to define the other arrays as _TEMPORARY_ and not give them individual field names.
Second: I don't see anything wrong immediately. I can't test it because of course I don't have access to cc_iv_sv. The only thing I can recommend is that you use the Debug option of the Data step and follow the code as it executes. If you have Enterprise Guide you would click on the little bug to enable debugging (if it is not already enabled). You have to have at least EG 7.1.3 I think (maybe it's 7.1.5) in order to use the EG debugger.
If you're using Display Manager ("PC SAS"), then you would add /Debug as part of your Data statement like so:
data want / debug;
If you're using SAS Studio, it's going to be roughly similar to Enterprise Guide, but I don't have access to SAS Studio at the moment, so I can't give you any instruction there.
If you're not familiar with the debugger, you can just Google something like "SAS debugger commands" and you should be able to get started.
Sorry I can't be of more assistance here, but I can't access cc_iv_sv, so there's not much more I can do.
Jim
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.