BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASuserlot
Barite | Level 11

I have some mean, n, and sd values, which I use to create a string.  The length of the string reaming the same in all conditions is important to me since I am adding another 'character based on the length of the string ( length in terms of visual). Is there any easy way to achieve this? Thank you for your input and suggestions.

data have;
input subjid n mean sd;
cards;
01 . . .
02 1 1.2 .
03 10 2.4 1.15
04 1 1.4 0.05
05 15 110.4 10.05
06 1 110.4 .
07 100 110.4 12.35
08 100 8.5 12.35
09 10 108.5 100.35

;
run; 
*
instruction:
1. if 'n' value missing then display value as '0'
2. if 'mean' or 'sd' missing then display the value ' ' ( based on the length to match string length)



*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 string1 = "n = XXX			'Total change in School in New York, Mean (SD): xxx.x (xxx.xx)"
 string2 = "n = XXX			'   Total change in School in Maine, Mean (SD): xxx.x (xxx.xx)"

The final string1 and string2 (79 characters including the blanks)
length should be same for all the subjects, Insert the blanks where it
needed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

But your code is using concatenation operator, ||, that will not remove any spaces.

Might be a place to use the goofy IFC() function.

....||ifc(missing(sd),'      ',put(sd,6.2))||...

View solution in original post

6 REPLIES 6
ballardw
Super User

Since this smells a lot like homework what  you have tried so far?

One approach is going to involve adding one piece at a time after determining the existing length of the combined value.

 

Numeric to character to concatenate character values is going to use PUT function with formats.

 

PS. Depending on how you look at these strings do not expect anything to line up as most of the fonts you are likely to use will be proportional which means multiple spaces display in different width.

 


@SASuserlot wrote:

I have some mean, n, and sd values, which I use to create a string.  The length of the string reaming the same in all conditions is important to me since I am adding another 'character based on the length of the string ( length in terms of visual). Is there any easy way to achieve this? Thank you for your input and suggestions.

data have;
input subjid n mean sd;
cards;
01 . . .
02 1 1.2 .
03 10 2.4 1.15
04 1 1.4 0.05
05 15 110.4 10.05
06 1 110.4 .
07 100 110.4 12.35
08 100 8.5 12.35
09 10 108.5 100.35

;
run; 
*
instruction:
1. if 'n' value missing then display value as '0'
2. if 'mean' or 'sd' missing then display the value ' ' ( based on the length to match string length)



*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 string1 = "n = XXX			'Total change in School in New York, Mean (SD): xxx.x (xxx.xx)"
 string2 = "n = XXX			'   Total change in School in Maine, Mean (SD): xxx.x (xxx.xx)"

The final string1 and string2 (79 characters including the blanks)
length should be same for all the subjects, Insert the blanks where it
needed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;

 

SASuserlot
Barite | Level 11

Ha ha. My instructions made you smell like 'Homework,' but it's not. Sorry if it felt like that.   I posted a similar query before which I got answers but didn't get what I am trying to achieve so, I am trying it in a different way.

here is what I tried.  My request is where the mean or n or sd values are missing. Do we have to just write 'IF' and 'ELSE' conditions to achieve as per my previous instructions?

if n =. then it displays as '0'  in the same format as other numbers

if mean  = . then id displays as the blank with the same format (length ) as other numbers and the same applies to sd. so that the string length remains the same.


data have;
input subjid n mean sd;
cards;
01 . . .
02 1 1.2 .
03 10 2.4 1.15
04 1 1.4 0.05
05 15 110.4 10.05
06 1 110.4 .
07 100 110.4 12.35
08 100 8.5 12.35
09 10 108.5 100.35

;
run; 

data want;
	length string1 $2500;
	set have;
	retain subjid n mean sd string1 string2;
string1= "n = "||put(n , 3.0)||"      Total change in School in New York, Mean (SD): "||put(mean 	,  	5.1)||" ("||put(sd  	,   6.2)||")";
string2= "n = "||put(n , 3.0)||"         Total change in School in Maine, Mean (SD): "||put(mean 	,  	5.1)||" ("||put(sd  	,   6.2)||")";
run;
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Result
n =  10      Total change in School in New York, Mean (SD):   2.4 (  1.15)--->string1
n =  10         Total change in School in Maine, Mean (SD):   2.4 (  1.15)--->string2

n = 100      Total change in School in New York, Mean (SD): 110.4 ( 12.35)--->string1
n = 100         Total change in School in Maine, Mean (SD): 110.4 ( 12.35)--->string2

n =   .      Total change in School in New York, Mean (SD):    .  (   .  )
n =   .         Total change in School in Maine, Mean (SD):    .  (   .  )

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;

 

 

Tom
Super User Tom
Super User

Looks like you solved it by just using PUT() with a fixed format.

Or did you want the period to disappear? 

 

Or are you asking how to right justify the variable length strings?  Use the -R modifier on the format specification.

put('Total change in School in New York, Mean (SD):',$53.-R)
SASuserlot
Barite | Level 11

Thank you for your feedback and suggestion. I need to insert the Blank spaces (maybe by invisible characters so that it won't get compressed with normal code). So that the length of the string stays the same as the bolded one


n = 100         Total change in School in Maine, Mean (SD): 110.4 ( 12.35)*--->string2- Standard length to compare;
n =   1         Total change in School in Maine, Mean (SD):   1.4  		  *--->when only SD missing;
n =   0         Total change in School in Maine, Mean (SD):               *--->when n, Mean, and SD missing;

Tom
Super User Tom
Super User

But your code is using concatenation operator, ||, that will not remove any spaces.

Might be a place to use the goofy IFC() function.

....||ifc(missing(sd),'      ',put(sd,6.2))||...
SASuserlot
Barite | Level 11

 Thank you, This is what I am looking for.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 6 replies
  • 505 views
  • 1 like
  • 3 in conversation