Help using Base SAS procedures

Format data in sas dataset

Accepted Solution Solved
Reply
Regular Contributor
Posts: 233
Accepted Solution

Format data in sas dataset

Below is my input
data have;
input col1 $4.;
cards;
abcd
efgh
ijkl
mnop
qrst
;
run;

Need output as follows
'abcd',
'efgh',
'ijkl',
'mnop',
'qrst'

Is this a possibility in SAS?


Accepted Solutions
Solution
‎04-16-2012 03:42 PM
Trusted Advisor
Posts: 1,301

Re: Format data in sas dataset

data foo;

input col1 $;

cards;

abcd

efgh

ijkl

mnop

qrst

;

run;

proc transpose data=foo out=bar(drop=_Smiley Happy; var col1; run;

filename tmp temp;

data _null_;

set bar;

file tmp dlm=',' dsd;

put (colSmiley Happy ($~);

run;

"abcd","efgh","ijkl","mnop","qrst"

*or;

data bar;

set foo;

col1=quote(strip(col1));

run;

*or;

data foo;

format col1 $quote.;

input col1 $;

cards;

abcd

efgh

ijkl

mnop

qrst

;

run;

proc print data=foo; run;

View solution in original post


All Replies
Super User
Posts: 19,822

Re: Format data in sas dataset

Is it the quoted part or each result on a separate line that you'd like?

And yes it's possible...

data _null_;

set sashelp.class;

file "C:\temp\test.csv" dlm=',' dsd;

put

name $~

sex $~

height $~

weight $~

age $~

;

run;

Regular Contributor
Posts: 233

Re: Format data in sas dataset

I need it for every observation. I only have one variable in the data set and it has to be in the ' ', format.

Super Contributor
Posts: 1,636

Re: Format data in sas dataset

how about this one?

data have;

input col1 $4.;

cards;

abcd

efgh

ijkl

mnop

qrst

;

run;

data want;

  set have;

  length n $8;

  n='*'||col1||'*';

  nn=translate(n,"'","*");

  run;

  proc print;run;

                            Obs    col1      n         nn

                           1     abcd    *abcd*    'abcd'

                           2     efgh    *efgh*    'efgh'

                           3     ijkl    *ijkl*    'ijkl'

                           4     mnop    *mnop*    'mnop'

                           5     qrst    *qrst*    'qrst'

Linlin

Regular Contributor
Posts: 233

Re: Format data in sas dataset

Thanks Linlin. It worked but I have to add , to the code.

data have;

input col1 $4.;

cards;

abcd

efgh

ijkl

mnop

qrst

;

run;

data want;

  set have;

  length n $8;

  n='*'||col1||'*'||',';

  nn=translate(n,"'","*");

  run;

  proc print;run;

Solution
‎04-16-2012 03:42 PM
Trusted Advisor
Posts: 1,301

Re: Format data in sas dataset

data foo;

input col1 $;

cards;

abcd

efgh

ijkl

mnop

qrst

;

run;

proc transpose data=foo out=bar(drop=_Smiley Happy; var col1; run;

filename tmp temp;

data _null_;

set bar;

file tmp dlm=',' dsd;

put (colSmiley Happy ($~);

run;

"abcd","efgh","ijkl","mnop","qrst"

*or;

data bar;

set foo;

col1=quote(strip(col1));

run;

*or;

data foo;

format col1 $quote.;

input col1 $;

cards;

abcd

efgh

ijkl

mnop

qrst

;

run;

proc print data=foo; run;

Super Contributor
Posts: 1,636

Re: Format data in sas dataset

I thought the OP asked for single quote.

Valued Guide
Posts: 765

Re: Format data in sas dataset

hi ,,, another idea ...

data want;

length col2 col3 $6;

set have;

col2 = catq('2a',col1);

col3 = catq('1a',col1);

run;

or, no LENGTH statement needed ...

data want;

set have;

col2 = put(col1,quote.);

col3 = translate(col2,"'",'"');

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 313 views
  • 3 likes
  • 5 in conversation