Desktop productivity for business analysts and programmers

Read only two column from Excel

Reply
Occasional Contributor
Posts: 17

Read only two column from Excel

hey guys,

I want to read a excel into SAS and just print two column of the table? I don't know how to do with this?

Grand Advisor
Posts: 17,336

Re: Read only two column from Excel

Just print two columns? So not an output data set, just a list?

Use a List task and select the two variables you would like to display.

Occasional Contributor
Posts: 17

Re: Read only two column from Excel

Just to print two columns as a table.

I read my data from library Li, I only want to get two columns: PHYSHLTH,TOLDHI2.   And PHYSHLTH >=5, at the same time TOLDHI2=2).

Can anyone help me with my code??

My code:

libname Li "C:\Users\yl665\downloads";

proc contents data=Li.llcp2011_abridged; run;

Data llcp2011_abridged;

Set llcp2011;

  When (PHYSHLTH >=5 and TOLDHI2=2);

RUN;

Proc print DATA llcp2011_abridged;run

Occasional Contributor
Posts: 17

Re: Read only two column from Excel

I read my data from library 1, I only want to get two columns: PHYSHLTH,TOLDHI2. and PHYSHLTH >=5, at the same time TOLDHI2=2).

Can anyone help me with my code??

My code:

libname Li "C:\Users\yl665\downloads";

proc contents data=Li.llcp2011_abridged; run;

Data llcp2011_abridged;

Set llcp2011;

  When (PHYSHLTH >=5 and TOLDHI2=2);

RUN;

Proc print DATA llcp2011_abridged;run

Grand Advisor
Posts: 17,336

Re: Read only two column from Excel

You've posted in EG, I assumed you wanted to use EG Tasks. For coding, its preferable to post in the Procedures Forum.

I have some uncertainties about your code, it seems disconnected though may be correct, see my questions below.

In general you can use VAR statement in proc print to keep variables, assuming you want to display VAR1 and VAR2 something like the following:

Data llcp2011_abridged; /*Is this data set in LI or created somewhere else?*/

Set llcp2011; /*This isn't related to the LI library above?*/

  When (PHYSHLTH >=5 and TOLDHI2=2); /*This should be a WHERE not WHEN*/

RUN;

Proc print DATA llcp2011_abridged; /*This is missing an = sign after the data*/

VAR VAR1 VAR2;

run;

Grand Advisor
Posts: 10,210

Re: Read only two column from Excel

If you have an existing data set and can express the records you want with variables in a single record then there is no need to create an additional dataset. Go directly to proc print with your data set and use a WHERE clause to select the records:

proc print data=Li.llcp2011; /* I put the Li there in case that is the library the dataset resides in you may want to add options NOOBS to suppress the number of observation that will appear with each record and LABEL to print the label instead of the variable name if you have labels assigned.*/

where Physhlth >= 5 and toldhi2=2;

var PHYSHLTH TOLDHI2;

run;

Ask a Question
Discussion stats
  • 5 replies
  • 355 views
  • 0 likes
  • 3 in conversation