A SAS BASE certification course for absolute SAS Beginners
Lanhu
Updated 2 days ago
DO LOOP

Hi Sharon, I noticed that in the examples of DO LOOP part, sometimes you put the capital/total value statement before the do statement, but sometimes you put it after the do statement. And in the quiz, I did notice there is a difference when I put the capital/total value before/after do statement. Could you explain what is the reason? Thank you very much!

sharon cheng
sharon — Instructor
· 2 days ago
Posted 2 days ago

Sure! For example, the code below is the solution for #1 in the coding exercise,

data salaryincrease1 (drop= counter);

Increase_rate = .03;

salary = 60000;

do counter = 1 to 5;

salary+salary*increase_rate;

Year+1;

output;

end;

format salary dollar10.2;

run;

Explanation for the 2 lines of code below:

salary = 60000;

this is outside the Do loop, it only runs 1 time at the beginning of the program. this means SAS sets Salary = 60000 at the beginning, this is the initial value for Salary

salary+salary*increase_rate;

this is inside the Do loop, and this Do loop will run 5 times, every time /iteration of the DO loop, salary = salary+salary*increase_rate

when counter = 1, salary = 60000+60000*0.03 --> salary after 1st iteration of DO loop

when counter = 2, salary = (60000+60000*0.03) + (60000+60000*0.03) *0.03 --> salary after 2nd iteration of DO loop

same logic applies to later iteration of DO loop.

The initial value 60000 is only assigned to Salary once at the outside of DO loop.

Hope this helps! Please let me know if you have further questions.

Honest and fair reviews are appreciated!

Regards,

Sharon



A SAS BASE certification course for absolute SAS Beginners

Sean
Updated 12 days ago
Question 4: If you concatenate the data sets below in the order shown, what is the value of Sale in observation 2 of the new data set?

Hi Sharon, no doubt its my fault and I'm not reading the question properly. ID 2 appears to be consistent in all the data sets. I don't understand why the answer is missing. Can you please clarify?

sharon cheng
sharon — Instructor
· 12 days ago Answer
Posted 12 days ago

When concatenating the data sets, even diff data sets have common ID vars, still the obs with same ID do not match-merge, instead they just concatenate like what is shown in the screenshot. For question 4, consider VarA is var Name in first dataset, and Var B is var Sale in the 2nd dataset. Hope this helps!



A SAS BASE certification course for absolute SAS Beginners Lecture - Coding Exercise 1: Solution

Alexander

Updated 25 days ago
Can I ask What is Wrong with my Code?

I have done all the appropriate coding and I then went to check the solution, mine is just as valid, so i don't understand what is wrong with mine, Here is my code

LIBNAME exrcs1 "/folders/myfolders";

DATA exrcs1.patient_hd_age;

infile "folders/myfolders/Patient_HD_age.txt";

INPUT

@1 Pid $ 1.

@2 Sdate mmddyy10.

@12 Edate mmddyy10.

@22 age 2.

;

run;

  1. NOTE: The SAS System stopped processing this step because of errors.
  2. WARNING: The data set EXRCS1.PATIENT_HD_AGE may be incomplete.When this step was stopped there were 0 observations and 4
  3. variables.
  4. WARNING: Data set EXRCS1.PATIENT_HD_AGE was not replaced because this step was stopped.

PROC PRINT data=exrcs1.patient_hd_age;

format Sdate date9.;

format Edate date9.;

run;

I also have tried using a directory of

infile "C:\SASUniversityEdition\myfolders\Patient_HD_age.txt"

there is an error of

  1. ERROR: Physical file does not exist, /opt/sasinside/SASConfig/Lev1/SASApp/folders/myfolders/Patient_HD_age.txt.
sharon cheng
sharon — Instructor
· 25 days ago
Posted 25 days ago

Sure! Please see my comments on you code in BOLD below, the parts that are needed to be corrected are also in BOLD.

LIBNAME exrcs1 "/folders/myfolders";

DATA exrcs1.patient_hd_age; /* --- Please do not use the same data set name as the input data set, especially they will be stored in same location*/

infile "folders/myfolders/Patient_HD_age.txt"; /* should add / before the 'folders' as the beginning*/

INPUT

@1 Pid $ 1.

@2 Sdate mmddyy10.

@12 Edate mmddyy10.

@22 age 2.

;

run;

I ran the corrected code below and everything works perfectly.

  1. LIBNAME exrcs1 "/folders/myfolders";
  2. DATA exrcs1.patienthdage;
  3. infile "/folders/myfolders/Patient_HD_age.txt";
  4. INPUT
  5. @1 Pid $ 1.
  6. @2 Sdate mmddyy10.
  7. @12 Edate mmddyy10.
  8. @22 age 2.
  9. ;
  10. run;

As for infile "C:\SASUniversityEdition\myfolders\Patient_HD_age.txt", this directory is for SAS Window environment, but NOT for SAS studio.

Hope this helps!

As always, honest and fair reviews/comments are really appreciated!

Regards,

Sharon


A SAS BASE certification course for absolute SAS Beginners Lecture - Using Conditional Clauses with the Iterative DO Statement
Luca
Updated 25 days ago
Mysterious statement

Hi Sharon,

Why do we need that IF statement after the loop? Since the loop works until years=10 or Capital>..., I don't understand how that IF could ever be true (and, therefore, needed).

Thanks!

Luca

sharon cheng
sharon — Instructor
· 5 months ago
Posted 5 months ago

Good question! When DO year reaches to 11, the iteration stops, however the year become 11, so the Year will shown as 11 instead of 10 in the final result, this is why I use IF statement to correct Year value to 10. Please see the results below for comparison. Hope this help!

data invest;

do year=1 to 10 until(Capital>=50000);

capital+2000;

capital+capital*.10;

end;

if year=11 then year=10;

run;

With IF statement, this is the final result:

Without IF statement, this is the final result:

Regards,

Sharon

Mark
· a month ago
Posted a month ago

Maybe to add a point:
The thing I didnt see was, that the output statment has the same effect as the if statement you discussed. So if you use the output statement you have the year=10 even without using the if statement.

sharon cheng
sharon — Instructor
· 25 days ago
Posted 25 days ago

If you want to use OUTPUT, you may use the following code (Please note I comment out the IF statement). But this will print out all 10 years' values instead of the last/final year's value. The result is pasted as well. Hope this helps!

  1. data invest;
  2. do year=1 to 10 until(Capital>=50000);
  3. capital+2000;
  4. capital+capital*.10;
  5. output;
  6. end;
  7. *if year=11 then year=10;
  8. run;




A SAS BASE certification course for absolute SAS Beginners Lecture - Coding Exercise 2: Solution

Nirav

Updated a month ago
DId not undertand the calculation for this one. If you could help with the addition part please.

Did not understand this calculation:

array state_sale (2) state_sale_CA state_sale_TX;

do i = 1 to 2;

state_sale (i) = 0;

do j=1 to 3;

state_sale{i}+sale{i,j}

sharon cheng
sharon — Instructor
· a month ago
Posted a month ago

This is a multiple dimensional array program. My explanations are in BOLD in the following program.

data sale2;

set sale1;

array sale (2,3) saleCA1 -- saleTX3;

/* (2,3) will separate the variables into the following pattern:

2 -- rows -- each row represent one state

3 -- columns --- each column represent one sale, such as column 1 represents sale 1, column 2 represents sale 2, column 3 represents sale 3

saleCA1 saleCA2 saleCA3

saleTX1 saleTX2 saleTX3 */

array state_sale (2) state_sale_CA state_sale_TX;

/*using this array to create 2 new variables state_sale_CA state_sale_TX, which will contain the total sale for that year for that state */

do i = 1 to 2;

/* This is the outer DO loop: i = 1 for state CA; i = 2 for state TX*/

state_sale (i) = 0;

/*set the initial total state sale to 0 so when a new iteration / new state occurs, the initial total state sale is 0*/

do j=1 to 3;

/*This is the inner DO LOOP, when i = 1 for state CA; state_sale (1) = 0; */

state_sale{i}+sale{i,j};

/* this is the same as state_sale(i) = state_sale{i}+sale{i,j};

so when j = 1: state_sale(1) = state_sale{1}+sale{1,1}= 0 + saleCA1

when j = 2: state_sale(1) = state_sale{1}+sale{1,1} + sale (1,2)= 0 + saleCA1+saleCA2

when j = 3: state_sale(1) = state_sale{1}+sale{1,1} + sale (1,2) + sale(1,3)= 0 + saleCA1+saleCA2+saleCA3

After the iteration when j = 3, the calculation/add up for CA total sale is done, and this is for the outer loop i = 1*/

/*Then when i = 2 this is for TX, same process goes on, the TX total sale will be added up in same process*/

end;

end;

run;

Hope this helps! Lecture 62 is on multiple dimensional array which include the detailed explanation on a similar example.

Regards,

Sharon



A SAS BASE certification course for absolute SAS Beginners Lecture - SAS functions: overview

Fatima
Updated a month ago
SAS functions

In the video, there were repeated columns for gender, total score and average score (without function and the other with a function). I know that there were repeated because of the functions. However, is it possible to include the functions at the beginning statements without writing a separate code?

For example:

DATA scoredata2;

set scoredata0;

TotalScore_func = sum (score1, score2, score3);

AverageScore_func = mean (score1, score2, score3);

run;

Is this possible?

How can gender only have one column with capital letter?

Thank you.

sharon cheng
sharon — Instructor
· a month ago
Posted a month ago

Yes, you don't have to include the following codes -- they are just for comparison.

  1. TotalScore = score1 + score2 + score3; /*missing values*/
  2. AverageScore = TotalScore/3;/*missing values*/

As for gender, the original data is gender = 'm' or 'f', I used UPCASE() function to convert the letters to capital letters in the following code.

Gender_func = UPCASE(gender);

Hope this helps!

Regards,

Sharon




The ultimate SAS Advanced certification course
Lecture - ORDER BY Clause: Sort Data
Yi
Omiting 'calculated'

Hi Sharon,

Here we use claculated column to sort dataset, but don't include the word 'calculated' in front. Why does this work? When can we omit the 'calculated'?

sharon cheng
sharon — Instructor
· a month ago
Updated a month ago

Good question!

I double-checked the most updated SAS 9.4 SQL procedure user's guide, for Order By, you can use an alias to refer to a calculated column.

I also tried adding the 'calculated' in front of the variable score_ave in Order By statement, the result and log are same as the code without the 'calculated'. So I think you don't have to use the 'calculated' for calculated column in Order By, but you can use it as it is easier to remember for the sake of consistence.

proc sql;

select *, sum (score1, score2, score3 )/3 as score_ave

from score_data

order by calculated score_ave;

quit;

Hope this helps!

Regards,

Sharon




The ultimate SAS Advanced certification course
Ines
Updated 2 months ago
Regarding Lecture 14

Hello Sharon, In the following code, I noticed that when we add the "order by gender" clause after the "group by" clause, this messes up with the aggregation output for the column class (6th column). Hence, the table is only aggregated by gender and not anymore by class.

I have also noticed similar results when we add and order by clause with respect to class or name:

proc sql;

select *, mean (score1, score2, score3 ) as score_mean,

max (calculated score_mean) as max_mean, min(calculated score_mean)as min_mean

from score_data_class

where calculated score_mean is not missing

group by gender, class

order by gender desc;

quit;

Two questions:

1. Would you know how to keep the aggregation by class valid when we add the order by clause?

2. Would you know how to aggregate by both gender+class and then order the names alphabetically within each category (gender+class)?

Thank you

sharon cheng
sharon — Instructor
· 2 months ago
Updated 2 months ago

Good questions!

1. order by gender desc;

This code only sorts/orders by Gender, not sorts by class, therefore within each gender group/value, class appears as un-sorted/un-ordered. You may use the following code to order both Gender and Class

order by gender desc, Class;

Below is the result:

2. Again, you only need to change the order by clause like below, you don't have to include the DESC option for Gender

proc sql;

select *, mean (score1, score2, score3 ) as score_mean,

max (calculated score_mean) as max_mean, min(calculated score_mean)as min_mean

from score_data_class

where calculated score_mean is not missing

group by gender, class

order by gender desc, class, name;

quit;

Hope this helps!

As always, I appreciate fair and honest review/rating.

Regards,

Sharon

Mark as top answer

Ines
· 2 months ago
Posted 2 months ago

Thanks a lot Sharon. This exactly answers my questions! Regards, Ines

Mark as helpful Mark as top answer
sharon cheng
sharon — Instructor
· 2 months ago
Posted 2 months ago

You are welcome!


The ultimate SAS Advanced certification course

Sean

Updated 2 months ago
Looking at Practical examples. 3. Computing Percentages within Subtotals - Can you please tell me why the percentage needs to be formatted as 8.2. Why doesn't 6.2 display the value properly?

calculated Count/Subtotal as Percent format=percent8.2

Also is there a easy way to remember when you need to put the full stop after a format? i.e. above there is no full stop after the w.d

sharon cheng
sharon — Instructor
· 2 months ago
Posted 2 months ago

Here is the info on Percent format, the BOLD part is the answer, that's why 6.2 only gives us 67% instead of 66.67%, I do want the values with 2 decimals.

PERCENTNw.d

Syntax Description

w

specifies the width of the output field.

Default:6

Range:4-32

Tip:The width of the output field must account for the minus sign ( - ), the percent sign ( % ), and a trailing blank, whether the number is negative or positive.

dspecifies the number of digits to the right of the decimal point in the numeric value. This argument is optional.

Range:0-31

Requirement:must be less than w

Hope this helps!

Regards,

Sharon