BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hubb
Calcite | Level 5

I am trying to decipher what exactly this code means...the more detailed you can help me answer this the better for me . Thanks so much!

 

libname Analysis "C:/Users/Desktop";



data output1 Analysis.output2;

     set input1;

     if date <= "01Apr2016"d then delete;

     if LHA = '11' then location = 'East Kootenay';

     else if LHA = '12' then location = 'Kootenay Boundary';

     else if LHA = '13' then location = 'Okanagan';

     else if LHA = '14' then location = 'TCS';

     else location = 'Non-IHA';

 

     if location not in ('Non-IHA') then output Analysis.output2;

     if location in ('Non-IHA') then output output1;

run;

 

proc sort data = Analysis.output2;

     by location;

run;

 

proc summary data =  Analysis.output2;

     by location;

     var patient_count;

     output out =  Analysis.output3 sum=;

run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

This code has to come from a SAS beginner, as it uses unnecessarily complicated constructs. Consider this:

data
  output1
  analysis.output2
; * this style of writing the data statement makes immediately clear that more than one dataset is created;
set input1; * use WORK.input1 as source;
where date > "01apr2016"d; * clearer than the if ... then delete;
select (LHA); * use this instead of a long if-then-else-if chain;
  when ('11') location = 'East Kootenay';
  when ('12') location = 'Kootenay Boundary';
  when ('13') location = 'Okanagan';
  when ('14') location = 'TCS';
  otherwise location = 'Non-IHA';
end; * the select block does a recoding of LHA to a new variable;
if location = 'Non-IHA' /* switch the output target according to LHA/location values */
then output output1;
else output analysis.output2;
run;

By using the format created by @Tom, you can replace the select block with a simple put statement.

View solution in original post

4 REPLIES 4
Astounding
PROC Star

hubb,

 

I don't think that anyone can teach you the basics of SAS by answering a question on a message board.  That work is up to you.  To get you started, here are a few questions to consider.  Try to answer each of them with a single sentence. 

 

What is the basic meaning of:

 

  • The DATA statement
  • The SET statement
  • an IF/THEN statement (OK to use two sentences here)
  • an ELSE statement
  • PROC SORT
  • PROC SUMMARY
  • a LIBNAME statement

 

It's OK to skip the OUTPUT statement for now.

hubb
Calcite | Level 5
I know Stata so I know how codes work. I also realize they’re trying to clean raw data into SAS suitable data, I just don’t understand what they’re trying to do by filtering data..if you can explain the details of what exactly is happening, that would be great.
Tom
Super User Tom
Super User

Throw away older data.

Recode LHA into new descriptive variable LOCATION.

Split the data into two output datasets, one for IHA locations and one for the Non_IHA locations.

 

Calculate the total of PATIENT_COUNT for each of the "IHA" locations.

 

You could get similar results in one step if you are willing to use the existing LHA values.  

proc summary nway data = input1 ;
  where date > "01Apr2016"d 
    and LHA in ('11','12','13','14')
  ;
  class lha ;
  var patient_count;
  output out =  Analysis.output3 sum=;
run;

If you want the LHA values to print in more human readable format then you could just define and attach a format.

proc format ;
 value $location 
   '11'='East Kootenay'
   '12'='Kootenay Boundary'
   '13'='Okanagan'
   '14'='TCS'
   other='Non-IHA'
 ;
run;

 

Kurt_Bremser
Super User

This code has to come from a SAS beginner, as it uses unnecessarily complicated constructs. Consider this:

data
  output1
  analysis.output2
; * this style of writing the data statement makes immediately clear that more than one dataset is created;
set input1; * use WORK.input1 as source;
where date > "01apr2016"d; * clearer than the if ... then delete;
select (LHA); * use this instead of a long if-then-else-if chain;
  when ('11') location = 'East Kootenay';
  when ('12') location = 'Kootenay Boundary';
  when ('13') location = 'Okanagan';
  when ('14') location = 'TCS';
  otherwise location = 'Non-IHA';
end; * the select block does a recoding of LHA to a new variable;
if location = 'Non-IHA' /* switch the output target according to LHA/location values */
then output output1;
else output analysis.output2;
run;

By using the format created by @Tom, you can replace the select block with a simple put statement.

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
  • 4 replies
  • 3503 views
  • 3 likes
  • 4 in conversation