BookmarkSubscribeRSS Feed
bayzid
Obsidian | Level 7

How can i find out maximum length of all the variables in a SAS dataset and then use those lengths to create a new dataset?

10 REPLIES 10
Astounding
PROC Star

You don't need to.  This program will create a new data set with zero observations, but using all variable definitions from the old data set (including lengths):

data new_dataset;
   stop;
   set old_dataset;
run;

If you need a printed version of the data set structure, use PROC CONTENTS.

data_null__
Jade | Level 19
Do you mean you want to resize the character variables to the length of the longest value?
bayzid
Obsidian | Level 7

Yes- I want to do that for both numeric and character variables. Some of the variables have greater length than maximum length. Using COMPRESS option in the dataset can reduce the size but do not change the variable length which creates an inflated dataset when transformed to other formats.

AhmedAl_Attar
Rhodochrosite | Level 12

Hi @bayzid 

You can use my attached macro (etl_shrinkMyData.sas) It does exactly what you are looking for.

%include "etl_shrinkmydata.sas"; 
%etl_shrinkMyData(p_inDsName=SASHELP.PRDSALE
, p_outDsName=WORK.shrunk, p_noCompress=);

Hope this helps

bayzid
Obsidian | Level 7

Hi Ahmed,
Thanks for the macro. It worked to reduce the variable length but the size of the dataset has been inflated >4 times.

AhmedAl_Attar
Rhodochrosite | Level 12

@bayzid 

That's most likely because your original data set was compressed while the newly shrunk data set has shorter record length.

You can run Proc Content against both data sets and compare the results.

 

If you compressed the shrunk data set, it will probably end up same size as the original if not smaller

bayzid
Obsidian | Level 7

Yes- that worked. After compressing the size is smaller than the original dataset. Is there any way to include the compress option in the macro so that it doesn't require an additional data step?

AhmedAl_Attar
Rhodochrosite | Level 12
Use
Options compress=yes;
before calling the macro
Tom
Super User Tom
Super User

@bayzid wrote:

Yes- that worked. After compressing the size is smaller than the original dataset. Is there any way to include the compress option in the macro so that it doesn't require an additional data step?


I didn't look at the source code of the macro, since it was shared as an attached instead of in the body of the message, but from the sample call you should be able to just add the COMPRESS= option to the output dataset name.

%etl_shrinkMyData
(p_inDsName=SASHELP.PRDSALE
,p_outDsName=WORK.shrunk(compress=yes)
,p_noCompress=
);

Or set the system option COMPRESS before calling the macro.

bayzid
Obsidian | Level 7

Yes- that worked.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 10 replies
  • 795 views
  • 1 like
  • 5 in conversation