DATA Step, Macro, Functions and more

What does this SAS Code mean?

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

What does this SAS Code mean?

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;

 


Accepted Solutions
Solution
‎12-09-2017 12:34 PM
Super User
Posts: 9,932

Re: What does this SAS Code mean?

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Super User
Posts: 6,644

Re: What does this SAS Code mean?

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.

New Contributor
Posts: 2

Re: What does this SAS Code mean?

Posted in reply to Astounding
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.
Super User
Super User
Posts: 7,944

Re: What does this SAS Code mean?

[ Edited ]

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;

 

Solution
‎12-09-2017 12:34 PM
Super User
Posts: 9,932

Re: What does this SAS Code mean?

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 345 views
  • 3 likes
  • 4 in conversation