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

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?