DATA Step, Macro, Functions and more

Conditionally adding a string to a character variable

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Conditionally adding a string to a character variable

I have a data file with a number of numeric variables with values of 0 or 1, as well as a few case summary character variables.  As a data checking procedure, we want to look at observations that have multiple variables with a value of 1.  While I could simply run a proc print of all of the numeric variables, I need to be able to distribute paper copies of the information and there are too many variables for a legible print.  My thought had been to create a new character variable in the data step and concatenate the name of each variable with a value of 1 onto it, but I cannot get that to work.

Here's what I have:

data checkfile; set xxx.xxx;

     length multi $140;

     if var1 = 1 then multi = "var1name";

     if var2 = 1 then multi = multi || " var2name";

     ...

     if varn = 1 then multi = multi || " varnname";

run;

When I run a proc freq, all I get in return is the results from the first if statement.  SAS isn't throwing up any red flags, so I'm at a bit of a loss as to what's going wrong.


Accepted Solutions
Solution
‎07-24-2012 11:12 AM
Super User
Posts: 11,343

Re: Conditionally adding a string to a character variable

Try using

if var2 = 1 then multi = catx(' ',multi,"var2name");

what is happening is multi was padded with blanks to make 140 characters and the || operator was attempting to append past that length. The CATX function will strip variables of leading and trailing blanks to make things fit better.

View solution in original post


All Replies
Solution
‎07-24-2012 11:12 AM
Super User
Posts: 11,343

Re: Conditionally adding a string to a character variable

Try using

if var2 = 1 then multi = catx(' ',multi,"var2name");

what is happening is multi was padded with blanks to make 140 characters and the || operator was attempting to append past that length. The CATX function will strip variables of leading and trailing blanks to make things fit better.

Regular Contributor
Posts: 151

Re: Conditionally adding a string to a character variable

There's actually a much easier way to achieve your goal, without having to write multiple IF statements.  Just store the var1-varn variables in an array, loop through each one to check for the value 1, then use the VNAME function to add that variable name to the concatenated list.  Your code will look something like this, I've put in 2 options for the 'multi' variable, the first stores the results as a comma separated list, the 2nd puts double quotes around each value as per your code.

data checkfile;

set xxx.xxx;

length multi $140;

array vars{*} var1--varn;

do i=1 to dim(vars);

if vars{i}=1 then call catx(',',multi,vname(vars{i}));

/* or */

if vars{i}=1 then call catx('',multi,quote(vname(vars{i})));

end;

drop i;

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 479 views
  • 0 likes
  • 3 in conversation