BookmarkSubscribeRSS Feed
Megan2020
Calcite | Level 5

Hi,

 The following code doesn't work. I'm wondering if someone could suggest a fix.

Note: I am trying to mimic an if-then statement with basically two conditions, e.g., if x then 1, if y then 2, else z. ..I can't figure out how to get it to do that as I'm not as proficient in macros.

 

%if &trim=0 %then %do;
%let condition = %str(if myvars(i)< perct1(i) then myvars(i)=perct1(i) );
%let condition = %str(if myvars(i)> perct2(i) then myvars(i)=perct2(i));
%end;
%else %let condition = %str(myvars(i)=min(perct2(i),max(perct1(i),myvars(i))) );

 

3 REPLIES 3
Reeza
Super User
Your logic and code don't match at all so I'm having a hard time understanding which to fix here.

What are you trying to do overall? What makes you think you need a macro in the first place as well - this type of logic is usually best done via a format or SELECT statement, not a macro but hard to confirm that without knowing more.
Megan2020
Calcite | Level 5
It's someone else' macro I'm trying to fix. Here's the entire macro

https://wrds-www.wharton.upenn.edu/pages/support/research-wrds/macros/wrds-macros-winsorize/#example



I need the by group functionality and his is the only one I can find that works for that but it only works for trimming TRIM=1 because I think he's missing code for TRIM=0.

That's what my few lines were trying to add.



Thanks for any help. Much appreciated.


Tom
Super User Tom
Super User

That %IF/%THEN/%ELSE is just being used to stuff some SAS code into a macro variable.

 

Is the problem that you want to stuff two SAS statements into the macro variable CONDITION when is TRIM is set to 0? 

Just add a semi-colon between the two statements inside the %STR() macro function call.

%if &trim=0 %then %let condition = 
%str(if myvars(i)< perct1(i) then myvars(i)=perct1(i) 
    ;if myvars(i)> perct2(i) then myvars(i)=perct2(i)
    )
;
%else %let condition = 
%str(myvars(i)=min(perct2(i),max(perct1(i),myvars(i))) 
    )
;

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 606 views
  • 2 likes
  • 3 in conversation