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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

2 REPLIES 2
ballardw
Super User

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.

Keith
Obsidian | Level 7

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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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