Learning SAS? Welcome to the exclusive online community for all SAS learners.

How to create Dataset from string

Reply
Occasional Contributor
Posts: 9

How to create Dataset from string

Hi,

I have a string and want to segregate the string into a dataset.

String=A1 A2 A3 A4 ACCT_ID ACCOUNT_NUMBER CUST_NO.

My Dataset look like this

Variable_Name

A1

A2

A3

A4

ACCT_ID

ACCOUNT_NUMBER

CUST_NO

I tried using below code using macro but it's creating column rather than row.

data var_name;

do i=1 to z;

variable_name=%scan(temp_var,&i,' ');

output;

run;

Let me know the solutionperer

Super User
Super User
Posts: 6,500

Re: How to create Dataset from string

Question does not make much sense.

Do you want to create a data set with those variables?  If so then were is the data? But without data you will have no rows in the dataset.

Super User
Posts: 5,084

Re: How to create Dataset from string

Here is an approach ... but you don't really tell us where to find the incoming STRING.  I'll assume it's in a macro variable:

data want;

   length variable_name $ 32;

   do _n_=1 to 7;

      variable_name = scan("&string", _n_, ' ');

      output;

   end;

run;

If this is a simplified program, and the list of variable names is actually much longer and difficult to count, the program can be modified to acccommodate that.

Good luck.     

N/A
Posts: 1

Re: How to create Dataset from string

Hello SAS_Programmer,

The following code also generate the desired output.

data inc;

a='A1 A2 A3 A4 ACCT_ID ACCOUNT_NUMBER CUST_NO';

run;

data inc1;

set inc;

do i = 1 to 7;

Variable_Name=scan(a,i,' ');

drop a i;

output;

end;

run;

proc print data=inc1;

run;

Frequent Contributor
Posts: 83

Re: How to create Dataset from string

Does your data look like this?

data;

  length a1 a2 a3 a4 acct_id cust  $30;

input a1 /a2 / a3 / a4 / acct_id / acc_num / cust;

datalines;

A1

A2

A3

A4

ACCT_a1

10000

1

A1

A2

A3

A4

acct_a2

2000

2

A1

A2

A3

A4

acct_a3

3000

3

;;;;;;

proc print;   run;

Valued Guide
Posts: 2,175

Re: How to create Dataset from string

Try this

data wanted ;

Input wanted :$33. @@ ;

if wanted =" " then stop;

cards;

A1 A2 A3 A4 ACCT_ID ACCOUNT_NUMBER CUST_NO

;

Ask a Question
Discussion stats
  • 5 replies
  • 650 views
  • 0 likes
  • 6 in conversation