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

I need to create a number of fields within a for loop.

Is it possible to name them based on what iteration the loop is on?

IE:

Run:

a,b,c,d,e,f

1,2,3,1,2,3

through this code:

integer x

     for x = 1 to fieldcount()

          begin

          if (fieldvalue(x) >= 2)

               begin

               String above(x)

               above(x) = fieldvalue(x)

               end

          end

and get an output of:

a,b,c,d,e,f,above2, above3, above5,above6

1,2,3,1,2,3,2,3,2,3

If that is not possible, is it possible to call variables based on the iteration of a loop?

IE:

Run:

a,b,c,d,e,f

1,2,3,1,2,3

through this code:

integer x

String above1

String above2

String above3

String above4

String above5

String above6

     for x = 1 to fieldcount()

          begin

          if (fieldvalue(x) >= 2)

               begin

               above(x) = fieldvalue(x)

               end

          end

and get an output of:

a,b,c,d,e,f,above1,above2, above3, above4, above5,above6

1,2,3,1,2,3,(null),2,3,(null),2,3


Before you ask, nulling out numbers below 2 (columns a and d) would not be an option. Those fields are used in later calculations.


Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
MikeFrost
SAS Employee

Hi,

To close the loop on this thread, I got the following response from one of our architects:

It is not possible to dynamically create fields at run-time inside an Expression node in a Data Management Platform Data Job. Even a construction like the sample code below does not work as declarations of fields are handled at “compile-time” and not at run-time:

   if condition

   begin

   // only create this field if
   the condition is true

   string special_field

   special_field=value

   end

There are functions though that allow you to interact dynamically with the set of fields that are available and allows you to loop through the list of defined fields, find data type and value of a field on an index, and set values. These functions would allow you to accomplish what is described in the blog posting. More details can be found in the Expression Language Reference Guide (see http://support.sas.com/documentation/onlinedoc/dfdmstudio/index.html), and look for functions like fieldcount(), fieldname(), fieldvalue(), fieldtype() and setfieldvalue() functions.

Here is a code snippet (taken from the above mentioned reference guide):

// Declare a hidden integer for the for loop,
initializing it to 0

hidden date field

hidden integer i

i = 0

hidden date Date_Field

// Checks each field to see if it is a date field

for i = 1 to FieldCount()

if FieldType(i) == 'Date' then

begin

Date_Field= FieldValue(i)

// If the date is in the future, then use
SETFIELDVALUE to set the value to null

if Date_Field > today()

SetFieldValue(i,null)

end

I hope that helps.

Mike F.

View solution in original post

3 REPLIES 3
bmedalen
Calcite | Level 5

MJames,

I just happen to see this post.  Did you figure out the answer to this Feb. 2013 question?

Thanks,

Brett

MikeFrost
SAS Employee

Hi,

To close the loop on this thread, I got the following response from one of our architects:

It is not possible to dynamically create fields at run-time inside an Expression node in a Data Management Platform Data Job. Even a construction like the sample code below does not work as declarations of fields are handled at “compile-time” and not at run-time:

   if condition

   begin

   // only create this field if
   the condition is true

   string special_field

   special_field=value

   end

There are functions though that allow you to interact dynamically with the set of fields that are available and allows you to loop through the list of defined fields, find data type and value of a field on an index, and set values. These functions would allow you to accomplish what is described in the blog posting. More details can be found in the Expression Language Reference Guide (see http://support.sas.com/documentation/onlinedoc/dfdmstudio/index.html), and look for functions like fieldcount(), fieldname(), fieldvalue(), fieldtype() and setfieldvalue() functions.

Here is a code snippet (taken from the above mentioned reference guide):

// Declare a hidden integer for the for loop,
initializing it to 0

hidden date field

hidden integer i

i = 0

hidden date Date_Field

// Checks each field to see if it is a date field

for i = 1 to FieldCount()

if FieldType(i) == 'Date' then

begin

Date_Field= FieldValue(i)

// If the date is in the future, then use
SETFIELDVALUE to set the value to null

if Date_Field > today()

SetFieldValue(i,null)

end

I hope that helps.

Mike F.

mjames
Calcite | Level 5

As Mike F said, it seems that this functionality is not possible. I'm certainly hoping that dynamic field creation is added in the future, but I'm not holding my breath.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1881 views
  • 1 like
  • 3 in conversation