BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Kathryn_SAS
SAS Employee

In the case of my code where I treat the macro variable as text and compare it to text or @Quentin code that uses a macro %IF condition, you will get a value of Company=B for every observation in the Class data set. The following code works for me:

%let cie=b2;

data class;
format company $1.;
set sashelp.class;
if "&cie" eq "b1" then company="A";
else if "&cie" eq "b2" then company ="B";
run;

proc print;
run;

Output:

Obs    company    Name       Sex    Age    Height    Weight

  1       B       Alfred      M      14     69.0      112.5
  2       B       Alice       F      13     56.5       84.0
  3       B       Barbara     F      13     65.3       98.0
  4       B       Carol       F      14     62.8      102.5
  5       B       Henry       M      14     63.5      102.5
  6       B       James       M      12     57.3       83.0
  7       B       Jane        F      12     59.8       84.5
  8       B       Janet       F      15     62.5      112.5
  9       B       Jeffrey     M      13     62.5       84.0
 10       B       John        M      12     59.0       99.5
 11       B       Joyce       F      11     51.3       50.5
 12       B       Judy        F      14     64.3       90.0
 13       B       Louise      F      12     56.3       77.0
 14       B       Mary        F      15     66.5      112.0
 15       B       Philip      M      16     72.0      150.0
 16       B       Robert      M      12     64.8      128.0
 17       B       Ronald      M      15     67.0      133.0
 18       B       Thomas      M      11     57.5       85.0
 19       B       William     M      15     66.5      112.0

If this is not working for you, please send your log.

Tom
Super User Tom
Super User

@alepage wrote:

The script below is not giving the expected result.

Please correct the script

 

/*%let cie=b2;*/
%let cie=b2;

data class;
format company $1.;
set sashelp.class;
if &cie eq b1 then company="A";
else if &cie eq b2 then company ="B";
run;

Just be your own macro processor and see what SAS code you asked the macro processor to create.  Your example would generate this data step.

data class;
format company $1.;
set sashelp.class;
if b2 eq b1 then company="A";
else if b2 eq b2 then company ="B";
run;

Since SASHELP.CLASS does not have variables named B1 or B2 the IF condition will be true since both variables will always have missing value.  So the new variable COMPANY will always be set to "A".

 

PS  Why did you feel the need to attach the $1. format to COMPANY?  It really won't have any impact on how SAS displays it since it will only be one byte long. Perhaps you meant to use this statement instead to define COMPANY as a character variable with a storage length of one byte.

length company $1;
Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 16 replies
  • 769 views
  • 6 likes
  • 7 in conversation