BookmarkSubscribeRSS Feed
chennupriya
Quartz | Level 8

HHi ,

instead of writing a code how do we add trailing blanks to alphanumeric fields using computed coloumn in SAS EG before exporting data into CSV file

6 REPLIES 6
Tom
Super User Tom
Super User

All SAS character strings already are padded on the right with blanks.

So most likely your question is about how to generate a non standard CSV file that includes normally unwanted trailing blanks.

Why would you want to make a file in such a format?

Do you have more information on the required format since it is clearly not a normal CSV file.

chennupriya
Quartz | Level 8

HHi

i have an alphanumeric field example : MM344

its format is $12. In properties now I need to add trailing blanks

and length shd be 25 . and then export that dataset 

as csv file

Tom
Super User Tom
Super User

So the FORMAT is just how the variable is displayed, but let's assume that your variable also has a length of 12.  You might get somewhere by just modifying the variable to use $25. format instead.  If you really need to change the length of the variable then you will need to create a new variable.

But a CSV file will NOT have the trailing spaces.  For example if you run this simple step you will see that the data for the character variable NAME does not have trailing spaces. The spaces are not needed because the commas serve as the delimiter between the fields. Hence the name Comma Separated Value (CSV).

682  data _null_;

683   set sashelp.class ;

684   file log dsd ;

685   put (_all_) (:);

686  run;

Alfred,M,14,69,112.5

Alice,F,13,56.5,84

Barbara,F,13,65.3,98

Carol,F,14,62.8,102.5

Henry,M,14,63.5,102.5

James,M,12,57.3,83

Jane,F,12,59.8,84.5

Janet,F,15,62.5,112.5

Jeffrey,M,13,62.5,84

John,M,12,59,99.5

Joyce,F,11,51.3,50.5

Judy,F,14,64.3,90

Louise,F,12,56.3,77

Mary,F,15,66.5,112

Philip,M,16,72,150

Robert,M,12,64.8,128

Ronald,M,15,67,133

Thomas,M,11,57.5,85

William,M,15,66.5,112

Tom
Super User Tom
Super User

Perhaps your requirement is to output a fixed column format file instead of a CSV file?  That is easy to so using a data step.  Not sure if EG has a task to do that however.

data _null_;

  set sashelp.class;

   file 'mynewfile.txt' ;

  put @1 name @26 age @30 sex ;

run;

chennupriya
Quartz | Level 8

CCan we use subpad function  but I am confused of which length I can specify and which format I can specify in code when source has format $12. , length 12 and now result dataset format A25 . Thinking how to represent in code

Tom
Super User Tom
Super User

If you are writing your own code then just define the variable to the length you want.

If you are writing a data step then use the LENGTH statement to define the variable.

data want ;

   set have ;

   length new $25;

   new=old;

run;

If you are writing SQL then use the LENGTH attribute

proc sql ;

   create table want as select *,old as new length=25 from have ;

quit;

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 3533 views
  • 0 likes
  • 2 in conversation