## SYMGET with LEFT function

Solved
Regular Contributor
Posts: 243

# SYMGET with LEFT function

Hi all,

I copied the following code from internet. Can someone please tell me what this LEFT function (see below in bold) do here? I actually don't know the use of left function. Thanks,

/*Duration of exercise each day of week;*/
%LET time1=2.0;
%LET time2=1.0;
%LET time3=0.5;
%LET time4=0.6;
%LET time5=0.2;
%LET time6=1.0;
%LET time7=1.2;
/*Exercise planned for each day of week ;*/
%let sport1 =bike;
%let sport2 =walk;
%let sport3 =jog;
%let sport4 =stretch;
%let sport5 =bike;
%let sport6 =swim;
%let sport7 =walk;

DATA eachday
wholeweek (KEEP = calsofar timesofar RENAME = (calsofar=calperweek timesofar=hrsperweek));
DO day = 1 TO 7;
LENGTH sport intensity \$8 ;
sport = SYMGET('sport'||LEFT(day)) ;
IF sport IN ('bike','swim','jog') THEN intensity = 'hard';
ELSE IF sport IN ('walk','dance') THEN intensity = 'moderate' ;
ELSE IF sport IN ('stretch','golf') THEN intensity = 'easy' ;
cal_num = INPUT(SYMGET(intensity),4.) ;
time = INPUT(SYMGET('time'||LEFT(day)),6.1) ;
totalcal = time*cal_num ;
calsofar = SUM(calsofar, totalcal) ;
timesofar = SUM(timesofar, time) ;
RETAIN calsofar timesofar ;
OUTPUT eachday ;
IF day = 7 THEN OUTPUT wholeweek ;
END;
RUN;

Accepted Solutions
Solution
‎11-12-2015 02:53 PM
Super User
Posts: 7,932

## Re: SYMGET with LEFT function

The LEFT() function left aligns the text in a character string. So basically it moves the leading blanks to trailing blanks.

In this case the programming is taking advantage of SAS's ability to automatically convert a numeric varaible DAY to a character string. But since SAS uses the BEST12. format when it does that there will be leading spaces in the result.

If you were wrting this code now you would use the CATS() function. This will avoid the need for the LEFT() funcaiton and also the annoying warning about automatic converstion.

sport = SYMGET(cats('sport',day));

All Replies
Solution
‎11-12-2015 02:53 PM
Super User
Posts: 7,932

## Re: SYMGET with LEFT function

The LEFT() function left aligns the text in a character string. So basically it moves the leading blanks to trailing blanks.

In this case the programming is taking advantage of SAS's ability to automatically convert a numeric varaible DAY to a character string. But since SAS uses the BEST12. format when it does that there will be leading spaces in the result.

If you were wrting this code now you would use the CATS() function. This will avoid the need for the LEFT() funcaiton and also the annoying warning about automatic converstion.

sport = SYMGET(cats('sport',day));

Posts: 1,147

## Re: SYMGET with LEFT function

left function will move the blank spaces in the beginning of the character variable day to right side. so that the values created will be like sport1,sport2,sport3..... if left is not used then there is a chance that the values created will be sport 1 , sport 2 ............. which will not resolve the macro variables to convert into values.
Thanks,
Jag
🔒 This topic is solved and locked.

Discussion stats
• 2 replies
• 327 views
• 1 like
• 3 in conversation