DATA Step, Macro, Functions and more

Psuedo Code

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Psuedo Code

Can any one knows how to convert below SAS to Pseudo code ?

 


sum ((case when (mod_r in ('02') and CAL IN ('P') and fec_in <= intnx('month', "PAR_FEC"d, -1, 'end') and EST not in ('7' '07' ' 7' '7 ' '70')
 and temp>0) then temp*SAL_OBL else . end)) as con_temp.

 


Accepted Solutions
Solution
Monday
Super User
Posts: 9,567

Re: Psuedo Code

Giving that awful spaghetti code some visual structure (and removing several unnecessary brackets) makes understanding it much easier:

sum (
  case
    when
      mod_r in ('02')
      and CAL IN ('P')
      and fec_in <= intnx('month', "PAR_FEC"d, -1, 'end')
      and EST not in ('7' '07' ' 7' '7 ' '70')
      and temp > 0
    then temp * SAL_OBL
    else . 
  end
) as con_temp

You assign a sum (over several observations, possibly determined by a group by option) to a new variable named con_temp.

This sum is built from the term "temp * SAL_OBL" whenever all the conditions in the when are met.

Hint: this function

intnx('month', "PAR_FEC"d, -1, 'end')

won't work, as the text PAR_FEC is no valid SAS data literal. I suspect this should be a macro variable call &PAR_FEC instead.

---------------------------------------------------------------------------------------------
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
Super User
Posts: 9,203

Re: Psuedo Code

Pseudocode just means sort of code.  There is no defined standard for what that should be.  What you provide there could be pseudocode for how to write an if statement in a datastep for instance.  So no, there is no way to provide something you expect which only you know.

Solution
Monday
Super User
Posts: 9,567

Re: Psuedo Code

Giving that awful spaghetti code some visual structure (and removing several unnecessary brackets) makes understanding it much easier:

sum (
  case
    when
      mod_r in ('02')
      and CAL IN ('P')
      and fec_in <= intnx('month', "PAR_FEC"d, -1, 'end')
      and EST not in ('7' '07' ' 7' '7 ' '70')
      and temp > 0
    then temp * SAL_OBL
    else . 
  end
) as con_temp

You assign a sum (over several observations, possibly determined by a group by option) to a new variable named con_temp.

This sum is built from the term "temp * SAL_OBL" whenever all the conditions in the when are met.

Hint: this function

intnx('month', "PAR_FEC"d, -1, 'end')

won't work, as the text PAR_FEC is no valid SAS data literal. I suspect this should be a macro variable call &PAR_FEC instead.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 10

Re: Psuedo Code

Posted in reply to KurtBremser

Hi 

Thanks a lot for your solution.Smiley Happy

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 94 views
  • 0 likes
  • 3 in conversation