BookmarkSubscribeRSS Feed
keen_sas
Quartz | Level 8

I have string with variable names and text embedded in it and all variable names are embedded in square brackets. While concatenating
need to cross check the result values present in the variable. In case of multiple results, these should be concatenated using comma  (addition of “and” before the last entry only). Variables are combination of character and numeric.

For example If A , B present then concatenation should be A and B. If 3 results are present result should be A, B and C .

 

text="The details of the student are (values=[NAME] [SEX] [AGE] [HEIGHT] [WEIGHT]).";

 

data class;
set sashelp.class;
/* scenarios to be considered*/
if name="Alice" then do ;call missing(height,weight);end;
if name="John" then do ; call missing(sex,age);end;
if name="Carol" then do ; call missing(age, height, weight) ;end;
ab=catx(',',name,sex, age, height, weight) ;
run ;

 

output should be as below.
The details of the student are Alfred,F,14,69 and 12.5.
The details of the student are John,59 and 99.5.
The details of the student are Carol and F.

 

 

Any suggestion or sample code how to get the required output. 

2 REPLIES 2
Amir
PROC Star

I have amended an extra row for William and assumed missing is set to '.'.

 

options missing = ' ';

data have;
   set sashelp.class;
   /* scenarios to be considered*/
   if name="Alice" then do ;call missing(height,weight);end;
   if name="John" then do ; call missing(sex,age);end;
   if name="Carol" then do ; call missing(age, height, weight) ;end;
   if name="William" then do ; call missing(sex, age, height, weight) ;end;
   ab=catx(',',name,sex, age, height, weight) ; 
run ;


data want;
   set have;

   length text   $ 100
          values $ 100
   ;

   values = catx(',',name,sex,age,height,weight);

   pos    = find(values,',',-length(strip(values)));

   if pos then
      values = catx(' ',substr(values,1,pos-1),'and',substr(values,pos+1));

   text = cat('The details of the students are ',strip(values),'.');
run;

options missing = '.';

 

Regards,

Amir.

 

ballardw
Super User

I will say unless this purely an exercise in programming logic that the output is problematic at best and potentially quite misleading.

Consider if an output is :

The details of the student are John and 80.

How do you know whether the 80 represents age, height or weight (given data similar to SASHELP.CLASS)?

 

If your actual data does not have any actual missing values that is one thing but random values without context when there are missing is very odd.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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