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

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'.
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

3 REPLIES 3
ballardw
Super User

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

ferris1973
Calcite | Level 5
I will try this thanks! Yes I was using the >10 to make it fire for testing!
Tom
Super User Tom
Super User

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.

SAS Innovate 2025: Register Now

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!

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
  • 3 replies
  • 662 views
  • 0 likes
  • 3 in conversation