BookmarkSubscribeRSS Feed
varam
Calcite | Level 5

I have data like this:

Race 

white                5

American         8

black or white  14 

 

that digits are the length of each record

 

how to assign max length of the record to the variable length by using a macro, please tell the logic how to solve

 

2 REPLIES 2
Reeza
Super User

Why a macro? What are you trying to do overall? What have you tried so far?

If you want to find the maximum of a numeric value you can use PROC MEANS or any data step or SQL. Depending on what you want to do afterwards will define which is the best approach.

EDIT: FYI - I moved your question from the developer community to the SAS New Users which seems more appropriate based on your question. 


@varam wrote:

I have data like this:

Race 

white                5

American         8

black or white  14 

 

that digits are the length of each record

 

how to assign max length of the record to the variable length by using a macro, please tell the logic how to solve

 


 

Patrick
Opal | Level 21

Best is to know your data and define variables based on this knowledge (like a pre-defined data dictionary).

If I understand your question then below code for what you've been asking for.

data have;
  input myvar $50.;
  datalines;
white
American
black or white
;

proc sql noprint;
  select max(lengthn(myvar)) into :myvar_max_len trimmed
  from have
  ;
quit;
%put &=myvar_max_len;


data want;
  length myvar $ &myvar_max_len;
  set have(rename=(myvar=_myvar));
  myvar=_myvar;
  drop _myvar;
run;

proc contents data=want;
run;

If you just want to reduce the variable length to save storage space then consider using option compress=yes as this will compress blanks when storing a SAS table on disk.

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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