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!
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,
I just happen to see this post. Did you figure out the answer to this Feb. 2013 question?
Thanks,
Brett
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.
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.