BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DavidPhillips2
Rhodochrosite | Level 12

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;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

19 REPLIES 19
PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
DavidPhillips2
Rhodochrosite | Level 12

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

ballardw
Super User

&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.

Astounding
PROC Star

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.

DavidPhillips2
Rhodochrosite | Level 12

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);

Astounding
PROC Star

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.

ballardw
Super User

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;

DavidPhillips2
Rhodochrosite | Level 12

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.

Tom
Super User Tom
Super User

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?

Tom
Super User Tom
Super User

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.

DavidPhillips2
Rhodochrosite | Level 12

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.

DavidPhillips2
Rhodochrosite | Level 12

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);

ballardw
Super User

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

DavidPhillips2
Rhodochrosite | Level 12

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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