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.
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.
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.
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: