Your SAS programs, embedded in web apps and elsewhere

IF statements with SET

Accepted Solution Solved
Reply
Super Contributor
Posts: 483
Accepted Solution

IF statements with SET

I have a structure like the below where I loop through multiple times.  The first time that I loop though I enter the if statement. The second time I loop through the if statement does not detect that &columnIdText = “&next_name”

I’ve tried the following.

Next_name = D5;

Next_name = “D5”;

Neither of which seems to work.

I’m thinking that the usage of the word SET changes the syntax requirements.

How can I make the if statement work?

DATA _NULL_;

FILE _WEBOUT;

SET &DATASET END=LAST;

IF &columnIdText="&next_name" THEN DO;

  next_name = D5;

END;

RUN;


Accepted Solutions
Solution
‎05-21-2015 04:28 PM
Super User
Posts: 10,516

Re: IF statements with SET

Try

if findw("&CHOICE",strip( DEGREE_STUD_LEVEL_ID))

the previous version had some blanks likely at the end of the variable not removed in the comparison

View solution in original post


All Replies
Trusted Advisor
Posts: 1,620

Re: IF statements with SET

Without seeing how the macro variables resolve and what's in your datasets, I doubt anyone can provide an answer.

Can you share with us a portion of your dataset, and what the macro variables resolve to?

What loop are you referring to?

Super Contributor
Posts: 483

Re: IF statements with SET

The Set… Run loops through the datset from the start of the dataset until the end of the dataset.

Super User
Posts: 10,516

Re: IF statements with SET

&columnIdText should resolve to a variable name. Is that variable in all of the data sets?

Is there a variable named D5 if all of the data sets?

Are you trying to set the value of a variable D5 to the value of Next_name or set Next_name to have the value "D5"?

%let Next_name=D5; would have the following behavior:

IF &columnIdText="&next_name" should be testing a variable to have the value D5 UNLESS you did something such as put quotes in &columnIdText. You don't show how that variable is created so guessing here.

IF &columnIdText=&next_name would be testing if a variable has the value of the variable D5.

Super User
Posts: 5,085

Re: IF statements with SET

The key to the answer:  give us an example of what the correct syntax would be for the DATA step if there were no macro language involved.

Super Contributor
Posts: 483

Re: IF statements with SET

proc sql;

CREATE TABLE ENUM_OPDS_STU_S_D_LEV_LIST (

  DEGREE_STUD_LEVEL_ID varchar(50)

);

  INSERT INTO ENUM_OPDS_STU_S_D_LEV_LIST (DEGREE_STUD_LEVEL_ID)

  VALUES ('D');

  INSERT INTO ENUM_OPDS_STU_S_D_LEV_LIST (DEGREE_STUD_LEVEL_ID)

  VALUES ('D1');

  INSERT INTO ENUM_OPDS_STU_S_D_LEV_LIST (DEGREE_STUD_LEVEL_ID)

  VALUES ('D2');

quit;

%let degStudLev = D D2;

%macro createDropdown(DATASET, columnIdText, CHOICE);

     %let i = 1;

     %let next_name = %scan(&CHOICE, &i);

     DATA _NULL_;

     SET &DATASET END=LAST;

     IF DEGREE_STUD_LEVEL_ID="&next_name" THEN DO;

            next_name = 'D2'; /*really I want to do a second scan here, but one step at a time, what syntax do I need on this line for the if statement to work a second time.*/

            put "made it";

     END;

    RUN;

%mend;

%createDropdown(ENUM_OPDS_STU_S_D_LEV_LIST, &degStudLev, &degStudLev);

Super User
Posts: 5,085

Re: IF statements with SET

Sorry, you'll need to get rid of the macro language.  For example, if &CHOICE is D D2, do you want:

data _null_;

    set mydataset end=last;

    if degree_stud_level_id="D" then do;

       next_name="D";

   end;

   else if degree_stud_level_id="D2" then do;

      next_name="D2";

   end;

run;

That's probably not the exact result you were aiming for, but whatever it is you will have to spell it out without macro language.

Super User
Posts: 10,516

Re: IF statements with SET

Are you looking  to set Next_name to 'D2' if DEGREE_STUD_LEVEL_ID is any of the values in Choice then this might be more appropriate:

if findw("&choice",DEGREE_STUD_LEVEL_ID) > 0 then do;

Or are you trying to set different values to next_name depending upon the value of DEGREE_STUD_LEVEL_ID?

BTW creating example datasets with SQL insert statements makes your examples look more complex. A simple datastep with input and datalines is simpler to add values for testing:

data ENUM_OPDS_STU_S_D_LEV_LIST;

   length DEGREE_STUD_LEVEL_ID $ 50; /* though 50 is sort of overkill with this data*/

   input DEGREE_STUD_LEVEL_ID;

datalines;

D

D1

D2

D5

;

run;

Super Contributor
Posts: 483

Re: IF statements with SET

I am looking to do something very close to:

“Are you looking to set Next_name to 'D2' if DEGREE_STUD_LEVEL_ID is any of the values in Choice then this might be more appropriate:

If the current value of next_name is found while looping through the dataset then I want to do an action and change the value of next_name so I can do the action again if the new value of next_name is found.


Findw will likely work researching.

Super User
Super User
Posts: 6,502

Re: IF statements with SET

This is very confusing.  Can you just make an example input data step and the desired output data step?

From the variable names it almost looks like you are trying to calculate if the student has finished the degree program.  I assume the multiple levels reflect steps along the way to completion?

Does the goal for each student vary?

Are you trying to just tally the results (make a report) or are you trying to make transactions on the data set to reflect completion?

Super User
Super User
Posts: 6,502

Re: IF statements with SET

Didn't you start this conversation with a list like D0 D1 D2?

So are trying to change the students status to the next in the list?

So first figure out a method to do what you want by writing out the code in full detail.  Perhaps something like

if status='D0' then new_status='D1';

else if status='D1' then new_status='D2';

....

Then see if there is a way to generate the code using macro.

Or re-imagine the problem so that it uses the data directly without code generation.

Super Contributor
Posts: 483

Re: IF statements with SET

I am using the code to produce a checked html dropdownlist with specified options checked.  I have a string that contains the values in the dropdownlist that should be checked.

E.g.

     D D2 should be checked.

     But D1 should not be checked.

My string for choice is D D2.

The lookup table for the dropdown contains the values D D1 D2.

Ballardw’s solution using

     if findw("&choice",DEGREE_STUD_LEVEL_ID) > 0 then do;

     where ,DEGREE_STUD_LEVEL_ID changes each loop iteration.

Should work.  I’m currently researching what I’m missing, its probably something simple involving which item should have “” around it.

Super Contributor
Posts: 483

Re: IF statements with SET

I simplified the code as far as it can be simplified.  I’m not sure why the findw line does not evaluate properly.  I need to be able to pass in degStudLev.  I tried a number of permutations including:

  if findw("&CHOICE", DEGREE_STUD_LEVEL_ID) > 0 then do;

  if findw("&CHOICE", "DEGREE_STUD_LEVEL_ID") > 0 then do;

_______________

proc sql;

CREATE TABLE ENUM_OPDS_STU_S_D_LEV_LIST (

  DEGREE_STUD_LEVEL_ID varchar(50)

);

  INSERT INTO ENUM_OPDS_STU_S_D_LEV_LIST (DEGREE_STUD_LEVEL_ID)

  VALUES ('D');

  INSERT INTO ENUM_OPDS_STU_S_D_LEV_LIST (DEGREE_STUD_LEVEL_ID)

  VALUES ('D1');

  INSERT INTO ENUM_OPDS_STU_S_D_LEV_LIST (DEGREE_STUD_LEVEL_ID)

  VALUES ('D2');

quit;

%let degStudLev = D D2;

%macro createDropdown(CHOICE);

  DATA _NULL_;

  SET ENUM_OPDS_STU_S_D_LEV_LIST END=LAST;

  if findw("&CHOICE", "DEGREE_STUD_LEVEL_ID") > 0 then do;

  put "made it";

  END;

  RUN;

%mend;

%createDropdown(&degStudLev);

Solution
‎05-21-2015 04:28 PM
Super User
Posts: 10,516

Re: IF statements with SET

Try

if findw("&CHOICE",strip( DEGREE_STUD_LEVEL_ID))

the previous version had some blanks likely at the end of the variable not removed in the comparison

Super Contributor
Posts: 483

Re: IF statements with SET

Ballardw,

Thanks for helping me out.  I’ll look over the strip API it should give me more information. Below is my dropdown that loads up with two criteria selected.

twocriteriapng.png

☑ This topic is SOLVED.

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

Discussion stats
  • 19 replies
  • 868 views
  • 6 likes
  • 5 in conversation