BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Hima
Obsidian | Level 7

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?

1 ACCEPTED SOLUTION

Accepted Solutions
FriedEgg
SAS Employee

data foo;

input col1 $;

cards;

abcd

efgh

ijkl

mnop

qrst

;

run;

proc transpose data=foo out=bar(drop=_:); var col1; run;

filename tmp temp;

data _null_;

set bar;

file tmp dlm=',' dsd;

put (col:) ($~);

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

7 REPLIES 7
Reeza
Super User

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;

Hima
Obsidian | Level 7

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

Linlin
Lapis Lazuli | Level 10

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

Hima
Obsidian | Level 7

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;

FriedEgg
SAS Employee

data foo;

input col1 $;

cards;

abcd

efgh

ijkl

mnop

qrst

;

run;

proc transpose data=foo out=bar(drop=_:); var col1; run;

filename tmp temp;

data _null_;

set bar;

file tmp dlm=',' dsd;

put (col:) ($~);

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;

Linlin
Lapis Lazuli | Level 10

I thought the OP asked for single quote.

MikeZdeb
Rhodochrosite | Level 12

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;

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 989 views
  • 3 likes
  • 5 in conversation