Arrangement of Variables in New SAS Dataset [duplicate] - sas

This question already has answers here:
SAS: rearrange field order in data step
(4 answers)
Closed 4 years ago.
I have a SAS dataset, in which I have variables in the following order
Customer_Name
Quantity_Sold
Total_Retail_Price
Product_Name
Supplier
Now, I want to create a new dataset based on this existing dataset but in the new dataset I want variable arrangement in the following order
Customer_Name
Supplier
Product_Name
Quantity_Sold
Total_Retail_Price
Can anyone help me to solve this problem
Thanks in Advance

You can use retain before SET to create a new data set.
data new;
retain Customer_ Name Supplier Product_Name Quantity_Sold Total_Retail_Price;
set have;
run;

Related

Create a column in SAS using a macro variable

I am trying to create a column using the string value of a macro variable in SAS.
I have a dataset called want7 which has a column called 'ID'. I want to create a new dataset called want8 with a new column called 'ID1' by dynamically linking it to &string1 (as in the name of the column is linked to &string1) but the values of the column should equal the value of the 'ID' column in want7. How do I do this? Thanks in advance. I have only copied and pasted what I could write since I am relatively new to SAS.
%let string1 = ID1;
data want8; set want7;
/*Something like &string1 = ID*
run;
Using sashelp.class as an example (because it exists by default). Substitute as needed:
%let string1 = ID1;
data want8;
set sashelp.class;
&string1 = age ;
run;
This will reread the dataset. If you just want renames, look at the dataset option rename=. See SAS documentation: https://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000695119.htm

How to reshape data wide to long [duplicate]

This question already has answers here:
SAS Data formatting (reverse proc transpose?)
(2 answers)
Closed 7 years ago.
I want to reshape data columns to rows
Initial Table as shown below
ID1 ID2 ID3 Name
----------------------------
I001 I002 I003 John
Desire Table like
ID Name
------------
I001 John
I002 John
I003 John
Can anyone help out?
Thanks lots!!
One way to do this is to set up an array of IDs and loop through with an explicit OUTPUT statement.
data want;
set have;
array ids(3) id1-id3;
do i=1 to dim(ids);
ID=ids(i);
OUTPUT;
end;
run;
You can use PROC TRANSPOSE Make sure your data is sorted by NAME
proc transpose data=have out=want(rename=(_name_=ID));
by Name;
run;

convert datetime to date in proc-sql sas [duplicate]

This question already has answers here:
SAS 9.3 DATETIME VARIABLE FORMAT AS DATE
(8 answers)
Closed 8 years ago.
I'm trying to convert datetime22.3 variable to ddmmmyy10. in proc sql, this is giving me ****** in the output column.
How can I get the correct values in the output column?
You need to convert original SAS DATETIME value (think of it as data type) to SAS DATE value using DATEPART() function and apply appropriate format:
proc sql;
create table work.abc
as select DISTINCT a.Account_Id,
DATEPART(a.Billing_Dt) format ddmmyy10. as Bill_date
from abc table;
quit;

SAS proc reg, need to set maximum number of regressors that enter model

I need help with proc reg in sas. Currently I'm using the following code:
proc reg data=input outest=data_output;
model y = x1-x25 / selection = cp;
run;
quit;
I wonder how to set maximum limit of number of regressors that enter my model. Now as you can see I want SAS to test 25 variables, but also I want it to select no more than 7 variables in my model.
And one more questions, does anybody now why SAS outputs only 601 model combinations when I use the procedure above? Why doesn't it show all possible models that it can create with this 25 regressors?
Any comments and help will be appreciated!
Use the STOP= option in the model statement.
proc reg data=input outest=data_output;
model y = x1-x25 / selection = cp stop=7;
run;
quit;

Update variable based on match in two tables

I have 2 tables, lets name them table1 and table2. Both of them have credit_id, loan_id and Date field. For some reason credit_id field needs to be updated with corresponding values from table2, linking data by Date and loan_id fields. To do so, I made a query like:
proc sql;
UPDATE a
SET a.credit_id = b.credit_id
FROM table1 a, table2 b
WHERE (a.Date = b.Date) AND (a.loan_id = b.loan_id);
quit;
According to googling, this query should work in many sql environments, but it seems that SAS is an exception here, because it seems that from part is ignored.
How to update needed field then?
I can't comment on the SQL, but you can do the same thing using a data step:
data table1;
update table1 table2(keep = date loan_id credit_id);
by date loan_id;
run;
This requires that:
No two rows in the same table have the same date and loan_id, and
Both tables are sorted/indexed by date and loan id
You need the keep on the transaction dataset in order to prevent it from updating/creating any other variables on the master dataset. There are also several other ways you could do this, e.g. using the modify or merge statements.