DATA Step, Macro, Functions and more

how to create a dataset

Reply
Contributor
Posts: 45

how to create a dataset

I have a row data like

THE NAME OF COUNTRY IS INDIA
THE NAME OF COUNTRY IS AMERICA
THE NAME OF COUNTRY IS LONDON
THE NAME OF COUNTRY IS CANADA

I have to make a data set which is like

name
INDIA
AMERICA
LONDON
CANADA

Please help me Message was edited by: arpit
Super Contributor
Super Contributor
Posts: 3,174

Re: how to create a dataset

Use the SCAN function with -1 as the second argument to select the last word.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search argument, this topic / post:

scan function site:sas.com
Contributor
Posts: 45

Re: how to create a dataset

The .csv file contain below data

THE NAME OF COUNTRY IS INDIA
THE NAME OF COUNTRY IS AMERICA
THE NAME OF COUNTRY IS LONDON
THE NAME OF COUNTRY IS CANADA



I have to create a dataset like

name
INDIA
AMERICA
LONDON
CANADA



can u explain me
Super Contributor
Super Contributor
Posts: 3,174

Re: how to create a dataset

From the data presented, it does not appear to be a "CSV" formatted file. What is unclear is whether the entire data-string is contained in one cell or multiple?

You can use SAS PROC IMPORT to read the external file -- this process will create a SAS database member/file for you. Then you can use a SAS DATA step with a SET to read your imported SAS file, and use a SAS character variable assignment statement. Code a LENGTH or ATTRIB statement to declare the new variable that will contain the sub-string value, and make use of the SCAN function, as shown below:

data _null_;
oldvar = "A B C D XXXXXXX';
length newvar $20;
newvar = scan(oldvar,-1,' ');
putlog _all_;
run;

Suggest using the Google searches below and review SAS-hosted documentation and supplemental technical / conference reference material.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search argument, this topic / post:

proc import csv introduction site:sas.com

data step programming introduction site:sas.com

data step scan function site:sas.com
Contributor
Posts: 45

Re: how to create a dataset

Thanks for your kind information

But how it use to make a data step like

INDIA
AMERICA
LONDON
CANADA Message was edited by: arpit
Frequent Contributor
Posts: 101

Re: how to create a dataset

There's a couple of ways you can do it. I'm using instream data in my code, but the data could be in a text file.

This is the method that Scott referred to in his post:

data method1;
input line $40.;
length country $8;
country = scan( line, -1 );
drop line;
datalines;
THE NAME OF COUNTRY IS INDIA
THE NAME OF COUNTRY IS AMERICA
THE NAME OF COUNTRY IS LONDON
THE NAME OF COUNTRY IS CANADA
;
run;


Another alternative:

data method2;
input @'THE NAME OF COUNTRY IS ' country $;
datalines;
THE NAME OF COUNTRY IS INDIA
THE NAME OF COUNTRY IS AMERICA
THE NAME OF COUNTRY IS LONDON
THE NAME OF COUNTRY IS CANADA
;
run;
Super User
Posts: 10,046

Re: how to create a dataset

Hi:
How about it:
[pre]
filename csv 'c:\test.csv';
data want;
infile csv length=len;
input whole $varying200. len;
country=scan(whole,-1);
drop whole;
run;
[/pre]



Ksharp
Ask a Question
Discussion stats
  • 6 replies
  • 188 views
  • 0 likes
  • 4 in conversation