How to count the length of a nonzero sequence in SASChange variable length in SAS datasetChanging values in previous and post records when a numerical condition is met using SASHow to create dummy variables to indicate two values are the same using SASHow to roll-up a column of data and separate values by a delimiter?Bring the length of an array to another array in SAS?Conditionally Counting a Response Across Multiple Variables in SASHow to count observations in a SAS dataset using proc sql?hLookup for the first non-zero value in SASCount the length of a chain in SQLSAS Counting Keywords Across Columns

How to travel to Japan while expressing milk?

How can a day be of 24 hours?

How to remove border from elements in the last row?

How can I prove that a state of equilibrium is unstable?

How to prevent "they're falling in love" trope

Why were 5.25" floppy drives cheaper than 8"?

Why was Sir Cadogan fired?

Can I hook these wires up to find the connection to a dead outlet?

Could the museum Saturn V's be refitted for one more flight?

If a warlock makes a Dancing Sword their pact weapon, is there a way to prevent it from disappearing if it's farther away for more than a minute?

Was the Stack Exchange "Happy April Fools" page fitting with the '90's code?

How to coordinate airplane tickets?

Did 'Cinema Songs' exist during Hiranyakshipu's time?

What is the fastest integer factorization to break RSA?

Do Iron Man suits sport waste management systems?

Why didn't Boeing produce its own regional jet?

How exploitable/balanced is this homebrew spell: Spell Permanency?

Why is the sentence "Das ist eine Nase" correct?

How to install cross-compiler on Ubuntu 18.04?

Finding the reason behind the value of the integral.

Convert seconds to minutes

Does int main() need a declaration on C++?

Knowledge-based authentication using Domain-driven Design in C#

Can a virus destroy the BIOS of a modern computer?



How to count the length of a nonzero sequence in SAS


Change variable length in SAS datasetChanging values in previous and post records when a numerical condition is met using SASHow to create dummy variables to indicate two values are the same using SASHow to roll-up a column of data and separate values by a delimiter?Bring the length of an array to another array in SAS?Conditionally Counting a Response Across Multiple Variables in SASHow to count observations in a SAS dataset using proc sql?hLookup for the first non-zero value in SASCount the length of a chain in SQLSAS Counting Keywords Across Columns













0















I'd like to count the length of the non zero sequence in a data as below:



ID Value 
1 0
1 0
1 2.5
1 3
1 0
1 4
1 2
1 5
1 0


So here the length of the first non zero sequence is 2 and the length of the second non zero sequence is 3. The new data will look like this:



ID Value Length 
1 0 0
1 0 0
1 2.5 2
1 3 2
1 0 0
1 4 3
1 2 3
1 5 3
1 0 0


How can I write SAS code to accomplish this task with a large data like this. Thanks!










share|improve this question
























  • Do you need to store each of the lengths? What is your desired output? A new dataset with a variable representing these lengths (down the column), or something else?

    – AlexK
    Mar 21 at 21:17











  • Yes. I need to store the length in a new variable. I edited my question as above.

    – Tammy
    Mar 21 at 21:46
















0















I'd like to count the length of the non zero sequence in a data as below:



ID Value 
1 0
1 0
1 2.5
1 3
1 0
1 4
1 2
1 5
1 0


So here the length of the first non zero sequence is 2 and the length of the second non zero sequence is 3. The new data will look like this:



ID Value Length 
1 0 0
1 0 0
1 2.5 2
1 3 2
1 0 0
1 4 3
1 2 3
1 5 3
1 0 0


How can I write SAS code to accomplish this task with a large data like this. Thanks!










share|improve this question
























  • Do you need to store each of the lengths? What is your desired output? A new dataset with a variable representing these lengths (down the column), or something else?

    – AlexK
    Mar 21 at 21:17











  • Yes. I need to store the length in a new variable. I edited my question as above.

    – Tammy
    Mar 21 at 21:46














0












0








0








I'd like to count the length of the non zero sequence in a data as below:



ID Value 
1 0
1 0
1 2.5
1 3
1 0
1 4
1 2
1 5
1 0


So here the length of the first non zero sequence is 2 and the length of the second non zero sequence is 3. The new data will look like this:



ID Value Length 
1 0 0
1 0 0
1 2.5 2
1 3 2
1 0 0
1 4 3
1 2 3
1 5 3
1 0 0


How can I write SAS code to accomplish this task with a large data like this. Thanks!










share|improve this question
















I'd like to count the length of the non zero sequence in a data as below:



ID Value 
1 0
1 0
1 2.5
1 3
1 0
1 4
1 2
1 5
1 0


So here the length of the first non zero sequence is 2 and the length of the second non zero sequence is 3. The new data will look like this:



ID Value Length 
1 0 0
1 0 0
1 2.5 2
1 3 2
1 0 0
1 4 3
1 2 3
1 5 3
1 0 0


How can I write SAS code to accomplish this task with a large data like this. Thanks!







sas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 0:05









AlexK

392111




392111










asked Mar 21 at 20:38









TammyTammy

12




12












  • Do you need to store each of the lengths? What is your desired output? A new dataset with a variable representing these lengths (down the column), or something else?

    – AlexK
    Mar 21 at 21:17











  • Yes. I need to store the length in a new variable. I edited my question as above.

    – Tammy
    Mar 21 at 21:46


















  • Do you need to store each of the lengths? What is your desired output? A new dataset with a variable representing these lengths (down the column), or something else?

    – AlexK
    Mar 21 at 21:17











  • Yes. I need to store the length in a new variable. I edited my question as above.

    – Tammy
    Mar 21 at 21:46

















Do you need to store each of the lengths? What is your desired output? A new dataset with a variable representing these lengths (down the column), or something else?

– AlexK
Mar 21 at 21:17





Do you need to store each of the lengths? What is your desired output? A new dataset with a variable representing these lengths (down the column), or something else?

– AlexK
Mar 21 at 21:17













Yes. I need to store the length in a new variable. I edited my question as above.

– Tammy
Mar 21 at 21:46






Yes. I need to store the length in a new variable. I edited my question as above.

– Tammy
Mar 21 at 21:46













1 Answer
1






active

oldest

votes


















0














Here is one possible solution. It assumes there are no missing values in the Value variable and that your ID variable does not have any significance for this problem.



*creates new length variable that starts at 1 and increments by 1 from start to end of every non-zero sequence;
data step_one (drop=prev_val);
set orig_data;
retain prev_val length 0;
indx = _n_;
if value ne 0 and prev_val ne 0 then length = length + 1;
else if value ne 0 then length = 1;
else if value = 0 then length = 0;
prev_val = value;
run;

*sorts dataset in reverse order;
proc sort data=step_one;
by descending indx;
run;

*creates modified length variable that carries maximum length value for each sequence down to all observations included in that sequence;
data step_two (drop=length prev_length rename=(length_new=length));
set step_one;
retain length_new prev_length 0;
if length = 0 then length_new = 0;
else if length ne 0 and prev_length = 0 then
length_new = length;
prev_length = length;
run;

*re-sorts dataset back to its original order and outputs final dataset with just the needed variables;
proc sort data=step_two out=final_result (keep=ID value length);
by indx;
run;





share|improve this answer























  • It works like a charm. Thank you so much!

    – Tammy
    Mar 22 at 14:57











  • You are welcome. Please mark the answer as accepted in that case.

    – AlexK
    Mar 22 at 18:57











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55288885%2fhow-to-count-the-length-of-a-nonzero-sequence-in-sas%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Here is one possible solution. It assumes there are no missing values in the Value variable and that your ID variable does not have any significance for this problem.



*creates new length variable that starts at 1 and increments by 1 from start to end of every non-zero sequence;
data step_one (drop=prev_val);
set orig_data;
retain prev_val length 0;
indx = _n_;
if value ne 0 and prev_val ne 0 then length = length + 1;
else if value ne 0 then length = 1;
else if value = 0 then length = 0;
prev_val = value;
run;

*sorts dataset in reverse order;
proc sort data=step_one;
by descending indx;
run;

*creates modified length variable that carries maximum length value for each sequence down to all observations included in that sequence;
data step_two (drop=length prev_length rename=(length_new=length));
set step_one;
retain length_new prev_length 0;
if length = 0 then length_new = 0;
else if length ne 0 and prev_length = 0 then
length_new = length;
prev_length = length;
run;

*re-sorts dataset back to its original order and outputs final dataset with just the needed variables;
proc sort data=step_two out=final_result (keep=ID value length);
by indx;
run;





share|improve this answer























  • It works like a charm. Thank you so much!

    – Tammy
    Mar 22 at 14:57











  • You are welcome. Please mark the answer as accepted in that case.

    – AlexK
    Mar 22 at 18:57















0














Here is one possible solution. It assumes there are no missing values in the Value variable and that your ID variable does not have any significance for this problem.



*creates new length variable that starts at 1 and increments by 1 from start to end of every non-zero sequence;
data step_one (drop=prev_val);
set orig_data;
retain prev_val length 0;
indx = _n_;
if value ne 0 and prev_val ne 0 then length = length + 1;
else if value ne 0 then length = 1;
else if value = 0 then length = 0;
prev_val = value;
run;

*sorts dataset in reverse order;
proc sort data=step_one;
by descending indx;
run;

*creates modified length variable that carries maximum length value for each sequence down to all observations included in that sequence;
data step_two (drop=length prev_length rename=(length_new=length));
set step_one;
retain length_new prev_length 0;
if length = 0 then length_new = 0;
else if length ne 0 and prev_length = 0 then
length_new = length;
prev_length = length;
run;

*re-sorts dataset back to its original order and outputs final dataset with just the needed variables;
proc sort data=step_two out=final_result (keep=ID value length);
by indx;
run;





share|improve this answer























  • It works like a charm. Thank you so much!

    – Tammy
    Mar 22 at 14:57











  • You are welcome. Please mark the answer as accepted in that case.

    – AlexK
    Mar 22 at 18:57













0












0








0







Here is one possible solution. It assumes there are no missing values in the Value variable and that your ID variable does not have any significance for this problem.



*creates new length variable that starts at 1 and increments by 1 from start to end of every non-zero sequence;
data step_one (drop=prev_val);
set orig_data;
retain prev_val length 0;
indx = _n_;
if value ne 0 and prev_val ne 0 then length = length + 1;
else if value ne 0 then length = 1;
else if value = 0 then length = 0;
prev_val = value;
run;

*sorts dataset in reverse order;
proc sort data=step_one;
by descending indx;
run;

*creates modified length variable that carries maximum length value for each sequence down to all observations included in that sequence;
data step_two (drop=length prev_length rename=(length_new=length));
set step_one;
retain length_new prev_length 0;
if length = 0 then length_new = 0;
else if length ne 0 and prev_length = 0 then
length_new = length;
prev_length = length;
run;

*re-sorts dataset back to its original order and outputs final dataset with just the needed variables;
proc sort data=step_two out=final_result (keep=ID value length);
by indx;
run;





share|improve this answer













Here is one possible solution. It assumes there are no missing values in the Value variable and that your ID variable does not have any significance for this problem.



*creates new length variable that starts at 1 and increments by 1 from start to end of every non-zero sequence;
data step_one (drop=prev_val);
set orig_data;
retain prev_val length 0;
indx = _n_;
if value ne 0 and prev_val ne 0 then length = length + 1;
else if value ne 0 then length = 1;
else if value = 0 then length = 0;
prev_val = value;
run;

*sorts dataset in reverse order;
proc sort data=step_one;
by descending indx;
run;

*creates modified length variable that carries maximum length value for each sequence down to all observations included in that sequence;
data step_two (drop=length prev_length rename=(length_new=length));
set step_one;
retain length_new prev_length 0;
if length = 0 then length_new = 0;
else if length ne 0 and prev_length = 0 then
length_new = length;
prev_length = length;
run;

*re-sorts dataset back to its original order and outputs final dataset with just the needed variables;
proc sort data=step_two out=final_result (keep=ID value length);
by indx;
run;






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 21 at 22:51









AlexKAlexK

392111




392111












  • It works like a charm. Thank you so much!

    – Tammy
    Mar 22 at 14:57











  • You are welcome. Please mark the answer as accepted in that case.

    – AlexK
    Mar 22 at 18:57

















  • It works like a charm. Thank you so much!

    – Tammy
    Mar 22 at 14:57











  • You are welcome. Please mark the answer as accepted in that case.

    – AlexK
    Mar 22 at 18:57
















It works like a charm. Thank you so much!

– Tammy
Mar 22 at 14:57





It works like a charm. Thank you so much!

– Tammy
Mar 22 at 14:57













You are welcome. Please mark the answer as accepted in that case.

– AlexK
Mar 22 at 18:57





You are welcome. Please mark the answer as accepted in that case.

– AlexK
Mar 22 at 18:57



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55288885%2fhow-to-count-the-length-of-a-nonzero-sequence-in-sas%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript