BookmarkSubscribeRSS Feed
monusing
Fluorite | Level 6

Please see the below SAS code and provide a explanation on why the value of variable jobcategory is "FA" only

 

data staff;
jobcategory = 'FA';
joblevel = '1';
jobcategory = jobcategory||joblevel;
run;

I am including a Length statement after data statement as "Length jobcategory $20.", still answer is same "FA".

 

but we are getting correct value once the output variable is changed to "JOBCATEGORY1" as shown below

data staff;
length jobcategory1 $15.;
jobcategory = 'FA';
joblevel = '1';
jobcategory1 = jobcategory||joblevel;
run;

Thanks in advance

3 REPLIES 3
Reeza
Super User

This is correct, because of trailing spaces.

 

In the first example it creates jobcategory with a length of 2 and then there's no room for the remaining text.

In the second example it creates jobcategory with a length of 15 - 2 chars and trailing spaces. 

 

I would assign a length at the beginning and use a CATT() function instead as it deals with the trailing spaces and I don't have to think about them. 

 

data staff;
length jobcategory $15.;
jobcategory = 'FA';
joblevel = '1';
jobcategory = catt(jobcategory, joblevel);
run;

proc print;run;
monusing
Fluorite | Level 6

 you want to say that when i assign the length as 15 initially, it includes  traling spaces also?.

So  length is a compiling time statement?

Shmuel
Garnet | Level 18

When first occurence of a variable is assignment of a string, like:

    data out;

       var = 'STRING';

       ....

then the length of that variable is the length of the string (in this example 6 chracters);

when you try to assign a longer string it is truncated.

 

To solve it you need define a length - the maximum expected for that variable;

        data out;

             length var $10;

             var = 'STRING';  output;

             var = 'Longer one'; output;

             var = 'Too mutch long string';  output;

       run;

 

      check out and you will see that the 3rd line is truncated to 10 characters only;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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