BookmarkSubscribeRSS Feed
apple
Calcite | Level 5

           data stud_Pri (keep =HSE_ID HH_NO Hsehold_Ref_No PriStd_cnt);

           set stud_dev;

                by Hsehold_Ref_No;

                if first.Hsehold_Ref_No then do;

                     where LVL_OF_EDN in ('0', '1');

                           PriStd_cnt=0; end;

                           PriStd_cnt + 1;

                if last.Hsehold_Ref_No then output;

                run;

I don't understand this code.

If first.Hsehold_Ref_No then do;

                     where LVL_OF_EDN in ('0', '1');

                           PriStd_cnt=0;end;

1) What if the first.Hsehold_Ref_No doesn't have LVL_OF_EDN in ('0', '1')? What will SAS do?

2) If the LVL_OF_EDN in ('0', '1') is in the 2nd or beyond record of the same Hsehold_Ref_No, will SAS count it?

Thank you

2 REPLIES 2
PGStats
Opal | Level 21

The WHERE statement is NOT an executable statement. This means that it should be moved out of the conditional block. Its effect is to restrict the input data to observations where LVL_OF_EDN is either '0' or '1'.

The datastep logic simply counts the number of observations with LVL_OF_EDN either '0' or '1' within each value of Hsehold_Ref_No.

It is assumed that the input dataset is sorted by Hsehold_Ref_No.

The datastep should read:

data stud_Pri (keep =HSE_ID HH_NO Hsehold_Ref_No PriStd_cnt);

set stud_dev;

by Hsehold_Ref_No;

where LVL_OF_EDN in ('0', '1');

if first.Hsehold_Ref_No then do;

  PriStd_cnt = 0;

  end;

PriStd_cnt + 1;

if last.Hsehold_Ref_No then output;

run;

PG

PG
PGStats
Opal | Level 21

Note: with your datastep, Hsehold_Ref_No groups having no observations with LVL_OF_EDN either '0' or '1' will be skipped, i.e. you will never get PriStd_cnt=0. If you want the zero counts, you should drop the WHERE statement and use :

data stud_Pri (keep =HSE_ID HH_NO Hsehold_Ref_No PriStd_cnt);

set stud_dev;

by Hsehold_Ref_No;

if first.Hsehold_Ref_No then do;

  PriStd_cnt = 0;

  end;

PriStd_cnt + (LVL_OF_EDN in ('0', '1'));

if last.Hsehold_Ref_No then output;

run;

PG

PG

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 401 views
  • 0 likes
  • 2 in conversation