I am struggling to put the value of an array into a put statement
You are just writing the value, not the name. You could just add the = to the put statement and SAS will include the variable name. While you are at it why not add the $QUOTE format so it is clearer where the value ends?
data test;
array newvar $20 var1 var2;
array oldvar $20 _var1 _var2;
input _var1 _var2;
subject+1;
do i=1 to dim(newvar);
newvar(i)=oldvar(i);
if length(oldvar(i)) >10 then
put 'ALERT P: ' newvar(I)= :$quote. 'is over 200 characters for ' subject=
;
end;
cards;
1 2
1234567890123 abc
xyz 123456789012
;
ALERT P: var1="1234567890123" is over 200 characters for subject=2 ALERT P: var2="123456789012" is over 200 characters for subject=3 NOTE: The data set WORK.TEST has 3 observations and 6 variables.
@ferris1973 wrote:
I am struggling to put the value of an array into a put statement
array newvar [*] var1 var2;
array oldvar [*] _var1 _var2;do i=1 to dim(newvar);
newvar(i)=oldvar(i);if length(oldvar(i)) >10 then put 'ALERT P: ' newvar(I) ' is over 200 characters for subject ' subject;
end;Each time I do this it resolves to value of var1 rather than 'var1'.
That is exactly what you are telling the program to do. Newvar(i) has the value of the oldvar array element. If you want to get the NAME of the variable then you want to use the VNAME function to get the name of the variable.
Something like:
do i=1 to dim(oldvar); varname = Vname(oldvar[i]); if length(oldvar(i)) >10 then put 'ALERT P: ' varname ' is over 200 characters for subject ' subject; end;
Though how you can tell that the length is over 200 when you are testing for 10 characters is questionable...
You are just writing the value, not the name. You could just add the = to the put statement and SAS will include the variable name. While you are at it why not add the $QUOTE format so it is clearer where the value ends?
data test;
array newvar $20 var1 var2;
array oldvar $20 _var1 _var2;
input _var1 _var2;
subject+1;
do i=1 to dim(newvar);
newvar(i)=oldvar(i);
if length(oldvar(i)) >10 then
put 'ALERT P: ' newvar(I)= :$quote. 'is over 200 characters for ' subject=
;
end;
cards;
1 2
1234567890123 abc
xyz 123456789012
;
ALERT P: var1="1234567890123" is over 200 characters for subject=2 ALERT P: var2="123456789012" is over 200 characters for subject=3 NOTE: The data set WORK.TEST has 3 observations and 6 variables.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.