Thanks for viewing and please share this with your SAS peeps. I welcome any comments/questions. Also, if you are not subscribed to the SAS Users UA-cam channel, you should be. There are some great how-to videos. Thanks again and good luck coding!
Hi there, I have a data where there is one original account number which has four duplicate accounts under it .. I want to check if these accounts are sorted in ascending order using loops
Hello, Aenugula, and thank you for your question! If you are trying to sort by account number, will PROC SORT work? You can learn more about it in our documentation: Overview: SORT Procedure 2.sas.com/6054HsC6e If not, we have two suggestions for better ways to get answers for a technical usage question like yours: 1) Our SAS Support Communities: these two posts will show you how and where to post your question: A) How to get fast, helpful answers 2.sas.com/6055HsC65 B) Where do I post my question in the SAS Support Communities? 2.sas.com/6056HsC6g 2) SAS Technical Support: if you are a licensed SAS user, we can also open a Technical Support track for you (let us know), or you can open your own by submitting this online form: 2.sas.com/6057HsC69 We hope that helps, and we wish you much ongoing success with SAS!
Hi Nishma. Great question. Our SAS Experts in the SAS Communities can certainly help you with this. Feel free to post your question and your data here: 2.sas.com/60543Re6I
Great! Do I use arrays if I want to do the following? I have COVID dataset like this patient_id collection_date test_type test_result 1 3/1/2020 Antibody Positive 1 3/3/2020 PCR Positive 1 3/14/2020 PCR Negative 2 2/12/2020 Antibody Negative 2 4/10/2020 Antibody Positive 3 6/10/2020 Antibody Positive 3 6/15/2020 PCR Negative I want to output to new table ONLY those patients that have Ab positive, but NO PCR done. So in this case, only patient_id 2 (second line showing Antibody Positive) should output because he had no PCR done.
Christian, thank you for your question! This may be a good resource to help you get started 2.sas.com/6054yxPR6 Since we can not post code and examples here, we recommend that you ask questions like this in our New SAS User community or SAS Software for Learning community. You can learn more about how to post in this video 2.sas.com/6055yxPRB
If there are 34 variables in each group don't understand why we iterate from 1 to 33 and not from 1 to 34? (i=0 to 33 instead of i=1 to 33). What happens if I were to look at that data set today? What would need to change in the code?
Michael - sorry for not seeing this question until today. I was prodded by the SAS Users person about your question and jumped right on to answer. There were actually (I believe) only 33 variables in each array (pos1-pos33), not 34. So all indexes should be 33 and the do loop should run from 1 to 33. I think I had a type in the DROP statement that ended with a 34. I apologize for any confusion!
That was a TYPO! This is why I try to minimize the typing that I have to do. I apologize for the confusion. All ending numbers in the DROP statement should have ended with a 33.
What would the difference be between 1) creating the 4 arrays then dropping them, and 2) making the arrays temporary using the _temporary_ option in the array statements?
Ben, You might want to read this article by SAS Tech Support about using Arrays. They have some good recommendations: 2.sas.com/6056GEdt8 . Most of the time, to answer a question like yours, you would need to benchmark both techniques to see which worked better as far as CPU usage, memory, or wall clock running time. One of our instructors indicated that you might save some time in not having to list variable names as array members with temporary arrays, but the amount of time saved would probably be fractional unless you had really large amounts of data that you were working with. Then it might become apparent which method was better than the other. This is really the type of question that can't be answered without benchmarking. Coding differences and coding maintenance are minimal. Temporary arrays are very handy for processing situations where you don't require variable names (for example, the temporary array is a list of goal numbers or rates to look up). But if you needed to create or reference numbered variables like year1-year50 or amount1-amount10, then using a temporary array wouldn't make as much sense. And if you have differently named variables (like AmtFinal2017, AmountF2018, F_Amt2019, and FinalAmt2020) that all need to be in an array, then a temporary array would not work for that scenario.
Hi! We looked into this for you and here's what we found. You can always provide an explicit number for the dimension when you define the array. Using the * just tells SAS to count the array elements from the variable list. This can be especially useful if you are defining different named variables as array members: "array products{*} house garden laundry clothing food beverage;" would cause the products array to have a dimension value of 6. Or, using a fixed number for the dimension value is always allowed. Of course, this means that you know how many elements will be in the array. Using a number for dimension that is less than the number of elements in the variable list would result in an error, such as "array pos(5) pos1-pos6;" for example. Here are some user group papers that discuss some array basic concepts: 2.sas.com/6059HDAqH and 2.sas.com/6050HDAqy . Hope that information helps!
Raghu, Unfortunately, we can't post code or answer Technical Support questions in this UA-cam feedback area. This type of question is best handled by SAS Tech Support. We suggest that you either open a track with SAS Tech Support or post your question and a sample of your data in the Community Forums. To open a track with Tech Support, fill out the form at this link: 2.sas.com/6057y0X0f . To post a programming question in the SAS Forums, visit this Forum and post here in the SAS Programming forum: 2.sas.com/6058y0X0A .
Why is a negative case considered a cases? Thought negative means No virus found and therefore wouldn't be considered a covid19 case? please clarify ... Thanks
Jennifer used data that are available here: covidtracking.com/data. This data includes testing data, which does record the number of tests that returned as "negative" for infection.
Hi Team, I am working on the restructure of the long to wide format and in the data I have data main(rename=visit=visitnum); do usubjid='101'; do visit=10,20,30,40,50; paramcd='ABC'; if visit=10 then aval=60; if visit=20 then aval=65; if visit=30 then aval=.; if visit=40 then aval=45; if visit=50 then aval=41; output; end; end; run; when I am restructuring with the below code: data trial1; set main; by usubjid paramcd; array visitn(5) visitn10 visitn20 visitn30 visitn40 visitn50 ; retain visitn; if first.paramcd then do ; do i=10,20,30,40,50; visitn(i)=.; end;end; visitn(visitnum)=aval; if last.paramcd; run; I am getting subscript error, how to resolve this . Your help is appreciated.
After checking with the presenter, please see the following: I saw two issues, one the rename= option in the data statement has the incorrect syntax, should be (rename=(oldname=newname)) The subscript is indeed out of range. There are only 5 elements in the array, so visitn{i} when 10 is substituted is looking for visit{10}, not visitn10. Since there are only 5 elements visitn{1} points to the variable visitn10, visitn{2} points to variable visitn20, visitn{3} points to visitn30, visitn{4} points to visitn40 and visitn{5} points to visitn50. So the do loop should be do i=1 to 5, not do i=10, 20, 30, 40, 50. I'm also not sure why they are trying to retain visitn, the array visitn is just a temporary grouping of the variables under one name visitn, it's not actually a variable that is getting created here, so in the test program, it's commented out. array visitn(5) visitn10 visitn20 visitn30 visitn40 visitn50; Here is a small test program you can run to see what happens so you can explain it better. data test; array visitn{50}; /*Creating 50 variables visitn1 to visitn50 */ do i=1 to 50; visitn{i}=2*i; end; drop i; run; data test2; set test; /* Array groups 5 variables listed under array named visitn with 5 elements */ array visitn(5) visitn10 visitn20 visitn30 visitn40 visitn50; *retain visitn; do i=1 to 5; visitn{i}=.; end; run;
Thanks for your positive feedback, Olga! We find the longer someone has been a #SASuser, the more beautiful they become! 😉 The effect seems to be magnified when they also share their knowledge of #SAS with others and volunteer to run and lead SAS related events, like SAS Global Forum! 🤩
This is really good! I never understood how my professor taught this in the classroom, but this is very easy to understand! Thanks!
Happy to hear that! Wishing you @SASsuccess
Thanks for viewing and please share this with your SAS peeps. I welcome any comments/questions. Also, if you are not subscribed to the SAS Users UA-cam channel, you should be. There are some great how-to videos. Thanks again and good luck coding!
Excellentl. Will use this to explain to the modelers I support.
This is fantastic, Jennifer! Thank you!
Roger, glad you found it helpful!
Oh my goodness. Amazing way to teach.
Thank you. It gives me some ideas in the step I am working now.
Awesome! Glad you found this tutorial helpful!
Very Good!!
Hi there,
I have a data where there is one original account number which has four duplicate accounts under it .. I want to check if these accounts are sorted in ascending order using loops
Hello, Aenugula, and thank you for your question!
If you are trying to sort by account number, will PROC SORT work? You can learn more about it in our documentation: Overview: SORT Procedure 2.sas.com/6054HsC6e
If not, we have two suggestions for better ways to get answers for a technical usage question like yours:
1) Our SAS Support Communities: these two posts will show you how and where to post your question:
A) How to get fast, helpful answers 2.sas.com/6055HsC65
B) Where do I post my question in the SAS Support Communities? 2.sas.com/6056HsC6g
2) SAS Technical Support: if you are a licensed SAS user, we can also open a Technical Support track for you (let us know), or you can open your own by submitting this online form: 2.sas.com/6057HsC69
We hope that helps, and we wish you much ongoing success with SAS!
Excellent! How do you restructure date, numerical and text data from long to short? Do you have any video?
Great question! We are looking into this for you.
You can check out PROC TRANSPOSE in this community topic: 2.sas.com/6055Gp3CH
Can you please help me to create vertical array and sum up backward based on some condition.??
Hi Nishma. Great question. Our SAS Experts in the SAS Communities can certainly help you with this. Feel free to post your question and your data here: 2.sas.com/60543Re6I
Great! Do I use arrays if I want to do the following?
I have COVID dataset like this
patient_id collection_date test_type test_result
1 3/1/2020 Antibody Positive
1 3/3/2020 PCR Positive
1 3/14/2020 PCR Negative
2 2/12/2020 Antibody Negative
2 4/10/2020 Antibody Positive
3 6/10/2020 Antibody Positive
3 6/15/2020 PCR Negative
I want to output to new table ONLY those patients that have Ab positive, but NO PCR done. So in this case, only patient_id 2 (second line showing Antibody Positive) should output because he had no PCR done.
Christian, thank you for your question! This may be a good resource to help you get started 2.sas.com/6054yxPR6 Since we can not post code and examples here, we recommend that you ask questions like this in our New SAS User community or SAS Software for Learning community. You can learn more about how to post in this video 2.sas.com/6055yxPRB
Thanks mam, so nice of u
If there are 34 variables in each group don't understand why we iterate from 1 to 33 and not from 1 to 34? (i=0 to 33 instead of i=1 to 33). What happens if I were to look at that data set today? What would need to change in the code?
Thanks for your question, Michael! We're checking on this for you, and will get back to you soon!
Michael - sorry for not seeing this question until today. I was prodded by the SAS Users person about your question and jumped right on to answer. There were actually (I believe) only 33 variables in each array (pos1-pos33), not 34. So all indexes should be 33 and the do loop should run from 1 to 33. I think I had a type in the DROP statement that ended with a 34. I apologize for any confusion!
Good lecture
You are wonderful!
We think you're referring to Jennifer Waller, and we happen to agree! Thanks for your comment, Laura!
Thanks! Madam
GREAT
Thank you so much!
Excellent! I was left with a doubt: Why did you request to drop until field "34"?
That was a TYPO! This is why I try to minimize the typing that I have to do. I apologize for the confusion. All ending numbers in the DROP statement should have ended with a 33.
What would the difference be between 1) creating the 4 arrays then dropping them, and 2) making the arrays temporary using the _temporary_ option in the array statements?
Thank you for your inquiry, Ben! We are checking on this for you!
Ben, You might want to read this article by SAS Tech Support about using Arrays. They have some good recommendations: 2.sas.com/6056GEdt8 . Most of the time, to answer a question like yours, you would need to benchmark both techniques to see which worked better as far as CPU usage, memory, or wall clock running time. One of our instructors indicated that you might save some time in not having to list variable names as array members with temporary arrays, but the amount of time saved would probably be fractional unless you had really large amounts of data that you were working with. Then it might become apparent which method was better than the other. This is really the type of question that can't be answered without benchmarking. Coding differences and coding maintenance are minimal. Temporary arrays are very handy for processing situations where you don't require variable names (for example, the temporary array is a list of goal numbers or rates to look up). But if you needed to create or reference numbered variables like year1-year50 or amount1-amount10, then using a temporary array wouldn't make as much sense. And if you have differently named variables (like AmtFinal2017, AmountF2018, F_Amt2019, and FinalAmt2020) that all need to be in an array, then a temporary array would not work for that scenario.
Great job!
Wonderful
high, I'm just wondering why I still succeeded when I had
array pos {*} pos1-pos6; rather than
array pos {6} pos1-pos6;
Hi there! We're looking into this for you and will get back to you with more info!
Hi! We looked into this for you and here's what we found. You can always provide an explicit number for the dimension when you define the array. Using the * just tells SAS to count the array elements from the variable list. This can be especially useful if you are defining different named variables as array members:
"array products{*} house garden laundry clothing food beverage;" would cause the products array to have a dimension value of 6. Or, using a fixed number for the dimension value is always allowed. Of course, this means that you know how many elements will be in the array. Using a number for dimension that is less than the number of elements in the variable list would result in an error, such as "array pos(5) pos1-pos6;" for example. Here are some user group papers that discuss some array basic concepts:
2.sas.com/6059HDAqH and 2.sas.com/6050HDAqy .
Hope that information helps!
@@SASUsers Thanks for your clear explanation!
Happy to help! 👍
how to do rotations(converting variables into observations-observations into variables) by using array-syntax
Raghu, Unfortunately, we can't post code or answer Technical Support questions in this UA-cam feedback area. This type of question is best handled by SAS Tech Support. We suggest that you either open a track with SAS Tech Support or post your question and a sample of your data in the Community Forums. To open a track with Tech Support, fill out the form at this link: 2.sas.com/6057y0X0f . To post a programming question in the SAS Forums, visit this Forum and post here in the SAS Programming forum: 2.sas.com/6058y0X0A .
Why is a negative case considered a cases? Thought negative means No virus found and therefore wouldn't be considered a covid19 case? please clarify ... Thanks
Jennifer used data that are available here: covidtracking.com/data. This data includes testing data, which does record the number of tests that returned as "negative" for infection.
This was probably just a naming issue. I likely should have named this totaltestsperformed and not totalcases. Sorry the confusion.
Hi Team,
I am working on the restructure of the long to wide format and in the data I have
data main(rename=visit=visitnum);
do usubjid='101';
do visit=10,20,30,40,50;
paramcd='ABC';
if visit=10 then aval=60;
if visit=20 then aval=65;
if visit=30 then aval=.;
if visit=40 then aval=45;
if visit=50 then aval=41;
output;
end;
end;
run;
when I am restructuring with the below code:
data trial1;
set main;
by usubjid paramcd;
array visitn(5) visitn10 visitn20 visitn30 visitn40 visitn50 ;
retain visitn;
if first.paramcd then do ;
do i=10,20,30,40,50;
visitn(i)=.;
end;end;
visitn(visitnum)=aval;
if last.paramcd;
run;
I am getting subscript error, how to resolve this .
Your help is appreciated.
Priyanka, we are checking on this for you!
After checking with the presenter, please see the following:
I saw two issues, one the rename= option in the data statement has the incorrect syntax, should be (rename=(oldname=newname))
The subscript is indeed out of range. There are only 5 elements in the array, so visitn{i} when 10 is substituted is looking for visit{10}, not visitn10.
Since there are only 5 elements visitn{1} points to the variable visitn10, visitn{2} points to variable visitn20, visitn{3} points to visitn30, visitn{4} points to visitn40 and visitn{5} points to visitn50. So the do loop should be
do i=1 to 5, not do i=10, 20, 30, 40, 50. I'm also not sure why they are trying to retain visitn, the array visitn is just a temporary grouping of the variables under one name visitn, it's not actually a variable that is getting created here, so in the test program, it's commented out.
array visitn(5) visitn10 visitn20 visitn30 visitn40 visitn50;
Here is a small test program you can run to see what happens so you can explain it better.
data test;
array visitn{50}; /*Creating 50 variables visitn1 to visitn50 */
do i=1 to 50;
visitn{i}=2*i;
end;
drop i;
run;
data test2;
set test;
/* Array groups 5 variables listed under array named visitn with 5 elements */
array visitn(5) visitn10 visitn20 visitn30 visitn40 visitn50;
*retain visitn;
do i=1 to 5;
visitn{i}=.;
end;
run;
@@SASUsers thank you so much for the reply Team
You are most welcome!
A fantastically beautiful woman and extremely useful information!
Thanks for your positive feedback, Olga!
We find the longer someone has been a #SASuser, the more beautiful they become! 😉 The effect seems to be magnified when they also share their knowledge of #SAS with others and volunteer to run and lead SAS related events, like SAS Global Forum! 🤩
Undoubtedly I observe this effect on myself!)) I absolutely agree!
Watch SAS base training day 1
ua-cam.com/video/kv3-rozR0U4/v-deo.html