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

I'm unable to understand the following code which was written by other programmer. Can someone help me understand this code in simple words and also I'd like to know if this can be converted to Proc SQL?

 

DATA ATT(DROP=J);*(DROP=FCST:);
  MERGE INVENT(IN=I)
        FCST_WIDE(IN=F);
  BY LOC MATERIAL;
  
  NEW_HAND = COALESCE(HAND,0);
  J=1;
  IF FIRST.MATERIAL THEN 
  DO;
    ARRAY FCT(*) &FCST_D2.;
  END;
  
  DO WHILE (NEW_HAND >0 and J <= DIM(FCT));
    
    FCT[J] = COALESCE(FCT[J],0);
    
    IF INPUT(SUBSTR(VNAME(FCT[J]),5),mmddyy10.)<=STOPSHIP_DT THEN 
    DO;
      
      IF NEW_HAND > FCT[J] THEN 
      DO;
        NEW_HAND = NEW_HAND-FCT[J];
        FCT[J]=0;
      END;
      ELSE IF FCT[J] > NEW_HAND THEN 
      DO;
        FCT[J] = FCT[J]-NEW_HAND;
        NEW_HAND=0;
      END;
      ELSE 
      DO;
        FCT[J]=0;
        NEW_HAND=0;
      END;
    END;
    J+1;
  END;
  
  IF LAST.MATERIAL THEN
  DO;
    REM_DEMAND = SUM(of FCST:);
  END;
RUN;


1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Which part(s) do you not understand? Without data, such as which variable(s) come from which data set(s) it can be a tad misleading to guess exactly. Especially since there is a macro variable without a definition apparently providing variable names.

If you have data look at the start and end result. That should help a lot.

 

ARRAY and SQL not going to happen. You have to explicitly code every thing for each variable. Since the attached code uses BY group processing to start/stop and summarize with First. and Last. processing, which requires processing records in a specific order, not to mention the way MERGE might differ from any type of SQL join then unless you have a great deal of time.

Apparently the data violates the best practice of not putting data in the variable names as there is a bit that is pulling date information from the variable names.

Since the data has data inside variable names it may be possible to extract that date information so that you have Date and value pairs and completely restructure the source data sets to a more normalized form and then join the data. Maybe. That would then allow possibly allow a join on dates as well as the LOC and Material variables. Again maybe.

View solution in original post

5 REPLIES 5
ballardw
Super User

Which part(s) do you not understand? Without data, such as which variable(s) come from which data set(s) it can be a tad misleading to guess exactly. Especially since there is a macro variable without a definition apparently providing variable names.

If you have data look at the start and end result. That should help a lot.

 

ARRAY and SQL not going to happen. You have to explicitly code every thing for each variable. Since the attached code uses BY group processing to start/stop and summarize with First. and Last. processing, which requires processing records in a specific order, not to mention the way MERGE might differ from any type of SQL join then unless you have a great deal of time.

Apparently the data violates the best practice of not putting data in the variable names as there is a bit that is pulling date information from the variable names.

Since the data has data inside variable names it may be possible to extract that date information so that you have Date and value pairs and completely restructure the source data sets to a more normalized form and then join the data. Maybe. That would then allow possibly allow a join on dates as well as the LOC and Material variables. Again maybe.

David_Billa
Rhodochrosite | Level 12
I'd like to understand array part and do while loop
PaigeMiller
Diamond | Level 26

@David_Billa wrote:
I'd like to understand array part and do while loop

What don't you understand about them?

--
Paige Miller
Kurt_Bremser
Super User

Maxim 1: Read the Documentation.

ARRAY 

DO WHILE 

 

The code suggests that there is data in structure (dates in variable names), which is always a VERY BAD IDEA and the main cause why array processing is needed. And this array processing is the #1 reason why you can't do this in SQL

This

IF FIRST.MATERIAL THEN 
  DO;
    ARRAY FCT(*) &FCST_D2.;
  END;

is total bogus and misleading to the unwary SAS novice, as ARRAY is a declarative statement which cannot be executed conditionally.

 

To get this mess straight, we need to see proper and useful (read: data step with datalines) examples of the datasets.

Reeza
Super User
Can you provide an example of the input data? It's not always straightforward to convert when the code is dynamic.

Just a guess, it looks like an expansion of data, possibly expanding a status of something over time.
Definitely not something I'd try with SQL.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 1367 views
  • 7 likes
  • 5 in conversation